View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.model.jdo;
18  
19  /***
20   * This interface provides constants denoting the treatment of null values 
21   * for persistent fields during storage in the data store.
22   *
23   * @author Michael Bouschen
24   */
25  public class NullValueTreatment 
26  {
27      /***
28       * Constant representing converting a null value of a field of nullable type 
29       * to the default value for the type in the datastore.
30       */
31      public static final int NONE = 0;
32  
33      /*** 
34       * Constant representing throwing an exception when storing a null value of 
35       * field of a nullable type that is mapped to non-nullable type in the 
36       * datastore.
37       */
38      public static final int EXCEPTION = 1;
39  
40      /***
41       * Constant representing converting a null value of a field of nullable type 
42       * to the default value for the type in the datastore.
43       */
44      public static final int DEFAULT = 2;
45  
46      /***
47       * Returns a string representation of the specified NullValueTreatment 
48       * constant.  
49       * @param nullValueTreatment the null value treatment, one of 
50       * {@link #NONE}, {@link #EXCEPTION} or {@link #DEFAULT}
51       * @return the string representation of the NullValueTreatment constant
52       */
53      public static String toString(int nullValueTreatment) 
54      {
55          switch (nullValueTreatment) {
56          case NONE :
57              return "none"; //NOI18N
58          case EXCEPTION :
59              return "exception"; //NOI18N
60          case DEFAULT :
61              return "default"; //NOI18N
62          default:
63              return "UNSPECIFIED"; //NOI18N
64          }
65      }
66  
67      /***
68       * Returns the NullValueTreatment constant for the string representation.
69       * @param nullValueTreatment the string representation of the null value
70       * treatment
71       * @return the null value treatment, one of {@link #NONE}, 
72       * {@link #EXCEPTION} or {@link #DEFAULT}
73       **/
74      public static int toNullValueTreatment(String nullValueTreatment)
75      {
76          if ((nullValueTreatment == null) || (nullValueTreatment.length() == 0))
77              return NONE;
78   
79          if ("none".equals(nullValueTreatment)) //NOI18N
80              return NONE;
81          else if ("exception".equals(nullValueTreatment)) //NOI18N
82              return EXCEPTION;
83          else if ("default".equals(nullValueTreatment)) //NOI18N
84              return DEFAULT;
85          else
86              return NONE;
87      }
88  }