View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.om.impl;
18  import java.util.Locale;
19  
20  import org.apache.jetspeed.om.common.LocalizedField;
21  import org.apache.jetspeed.util.HashCodeBuilder;
22  import org.apache.jetspeed.util.JetspeedLongObjectID;
23  import org.apache.pluto.om.common.ObjectID;
24  
25  /***
26   * LocalizedFieldImpl
27   * <br/>
28   * Implementation that represents a string value and the locale of that string
29   * 
30   * @author <a href="mailto:jford@apache.org">Jeremy Ford</a>
31   * @version $Id: LocalizedFieldImpl.java 516448 2007-03-09 16:25:47Z ate $
32   *
33   */
34  public class LocalizedFieldImpl implements LocalizedField
35  {
36      protected String value;
37      protected String name;
38      protected Locale locale;
39      
40      protected long parentId;
41      protected JetspeedLongObjectID id;
42      
43      public LocalizedFieldImpl()
44      {
45          
46      }
47      
48      public LocalizedFieldImpl(Locale locale, String value)
49      {
50          this.locale = locale;
51          this.value = value;
52      }
53  
54      /* (non-Javadoc)
55       * @see org.apache.jetspeed.om.common.LocalizedField#getLocale()
56       */
57      public Locale getLocale()
58      {
59          return locale;
60      }
61  
62      /* (non-Javadoc)
63       * @see org.apache.jetspeed.om.common.LocalizedField#setLocale(java.util.Locale)
64       */
65      public void setLocale(Locale locale)
66      {
67          this.locale = locale;        
68      }
69  
70      /* (non-Javadoc)
71       * @see org.apache.jetspeed.om.common.LocalizedField#getValue()
72       */
73      public String getValue()
74      {
75          return value;
76      }
77  
78      /* (non-Javadoc)
79       * @see org.apache.jetspeed.om.common.LocalizedField#setValue(java.lang.String)
80       */
81      public void setValue(String value)
82      {
83          this.value = value;        
84      }
85  
86      /***
87       * 
88       */
89      public ObjectID getId()
90      {
91          return id;
92      }
93  
94      public void setLanguage(String language)
95      {
96  		if (language != null)
97          {
98              String[] localeArray = language.split("[-|_]");
99              String country = "";
100             String variant = "";
101             for (int i = 0; i < localeArray.length; i++)
102             {
103                 if (i == 0)
104                 {
105                     language = localeArray[i];
106                 }
107                 else if (i == 1)
108                 {
109                     country = localeArray[i];
110                 }
111                 else if (i == 2)
112                 {
113                     variant = localeArray[i];
114                 }
115             }
116 
117             this.locale = new Locale(language, country, variant);
118         }
119     }
120 
121     public String getLanguage()
122     {
123         if (this.locale != null)
124         {
125             return this.locale.toString();
126         }
127         return null;
128     }
129 
130     /* (non-Javadoc)
131      * @see org.apache.jetspeed.om.common.LocalizedField#getName()
132      */
133     public String getName()
134     {
135         return name;
136     }
137 
138     /* (non-Javadoc)
139      * @see org.apache.jetspeed.om.common.LocalizedField#setName(java.lang.String)
140      */
141     public void setName(String name)
142     {
143         this.name = name;        
144     }
145     
146     public String toString()
147     {
148         return "Name: " + name + " Value: " + value + " Locale: " + locale;
149     }
150     
151     public boolean equals(Object o)
152     {
153         boolean result = false;
154         
155         if(o instanceof LocalizedFieldImpl && o != null)
156         {
157             LocalizedFieldImpl localField = (LocalizedFieldImpl)o;
158             
159             result = (this.name == null) ? (localField.name == null) : (this.name.equals(localField.name));
160             result = result && ((this.value == null) ? (localField.value == null) : (this.value.equals(localField.value)));
161             result = result && ((this.locale == null) ? (localField.locale == null) : (this.locale.equals(localField.locale)));
162         }
163         
164         return result;
165     }
166     
167     /***
168      * @see java.lang.Object#hashCode()
169      */
170     public int hashCode()
171     {
172         HashCodeBuilder hasher = new HashCodeBuilder(27, 101);
173         hasher.append(name).append(value);
174         if(locale != null)
175         {    
176             hasher.append(locale.getCountry()).append(locale.getLanguage()).append(locale.getVariant());
177         }
178         return hasher.toHashCode();
179     }
180 }