KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > internal > model > Value


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.core.internal.model;
22
23
24 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
25 import org.apache.directory.ldapstudio.browser.core.internal.search.LdapSearchPageScoreComputer;
26 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
27 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
28 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
29 import org.apache.directory.ldapstudio.browser.core.model.IValue;
30 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
31 import org.apache.directory.ldapstudio.browser.core.model.RDNPart;
32 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
33 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
34 import org.eclipse.search.ui.ISearchPageScoreComputer;
35
36
37 /**
38  * Default implementation of IValue.
39  *
40  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  * @version $Rev$, $Date$
42  */

43 public class Value implements IValue
44 {
45
46     /** The serialVersionUID. */
47     private static final long serialVersionUID = -9039209604742682740L;
48
49     /** The attribute this value belongs to */
50     private IAttribute attribute;
51
52     /** The raw value, either a String or a byte[] */
53     private Object JavaDoc rawValue;
54
55
56     /**
57      * Creates a new instance of Value.
58      *
59      * @param attribute the attribute this value belongs to
60      * @param rawValue the raw value, either a String or a byte[]
61      * @throws ModelModificationException if one of the parameter is null
62      */

63     public Value( IAttribute attribute, Object JavaDoc rawValue ) throws ModelModificationException
64     {
65         this.init( attribute, rawValue );
66
67         if ( rawValue == null )
68         {
69             throw new ModelModificationException( BrowserCoreMessages.model__empty_value );
70         }
71     }
72
73
74     /**
75      * Creates a new instance of Value with an empty value.
76      *
77      * @param attribute the attribute this value belongs to
78      * @throws ModelModificationException if one of the parameter is null
79      */

80     public Value( IAttribute attribute ) throws ModelModificationException
81     {
82         this.init( attribute, null );
83     }
84
85
86     /**
87      * Initializes this Value.
88      *
89      * @param attribute the attribute this value belongs to
90      * @param rawValue the raw value, either a String or a byte[] or null
91      * @throws ModelModificationException
92      */

93     private void init( IAttribute attribute, Object JavaDoc rawValue ) throws ModelModificationException
94     {
95         if ( attribute == null )
96         {
97             throw new ModelModificationException( BrowserCoreMessages.model__empty_attribute );
98         }
99
100         this.attribute = attribute;
101
102         if ( rawValue == null )
103         {
104             if ( attribute.isString() )
105             {
106                 this.rawValue = IValue.EMPTY_STRING_VALUE;
107             }
108             else
109             {
110                 this.rawValue = IValue.EMPTY_BINARY_VALUE;
111             }
112         }
113         else
114         {
115             this.rawValue = rawValue;
116         }
117     }
118
119
120     /**
121      * {@inheritDoc}
122      */

123     public IAttribute getAttribute()
124     {
125         return this.attribute;
126     }
127
128
129     /**
130      * {@inheritDoc}
131      */

132     public Object JavaDoc getRawValue()
133     {
134         return this.rawValue;
135     }
136
137
138     /**
139      * {@inheritDoc}
140      */

141     public String JavaDoc getStringValue()
142     {
143
144         if ( this.rawValue == EMPTY_STRING_VALUE )
145         {
146             return EMPTY_STRING_VALUE.getStringValue();
147         }
148         else if ( this.rawValue == EMPTY_BINARY_VALUE )
149         {
150             return EMPTY_BINARY_VALUE.getStringValue();
151         }
152         else if ( this.rawValue instanceof String JavaDoc )
153         {
154             return ( String JavaDoc ) this.rawValue;
155         }
156         else if ( this.rawValue instanceof byte[] )
157         {
158             return LdifUtils.utf8decode( ( byte[] ) this.rawValue );
159         }
160         else
161         {
162             return "UNKNOWN";
163         }
164     }
165
166
167     /**
168      * {@inheritDoc}
169      */

170     public byte[] getBinaryValue()
171     {
172         if ( this.rawValue == EMPTY_STRING_VALUE )
173         {
174             return EMPTY_STRING_VALUE.getBinaryValue();
175         }
176         else if ( this.rawValue == EMPTY_BINARY_VALUE )
177         {
178             return EMPTY_BINARY_VALUE.getBinaryValue();
179         }
180         else if ( this.rawValue instanceof byte[] )
181         {
182             return ( byte[] ) this.rawValue;
183         }
184         else if ( this.rawValue instanceof String JavaDoc )
185         {
186             return LdifUtils.utf8encode( ( String JavaDoc ) this.rawValue );
187         }
188         else
189         {
190             return LdifUtils.utf8encode( "UNKNOWN" );
191         }
192     }
193
194
195     /**
196      * {@inheritDoc}
197      */

198     public boolean isString()
199     {
200         return this.rawValue == EMPTY_STRING_VALUE || this.attribute.isString();
201     }
202
203
204     /**
205      * {@inheritDoc}
206      */

207     public boolean isBinary()
208     {
209         return this.rawValue == EMPTY_BINARY_VALUE || this.attribute.isBinary();
210     }
211
212
213     /**
214      * {@inheritDoc}
215      */

216     public boolean isEmpty()
217     {
218         return this.rawValue == EMPTY_STRING_VALUE || this.rawValue == EMPTY_BINARY_VALUE;
219     }
220
221
222     /**
223      * {@inheritDoc}
224      */

225     public boolean equals( Object JavaDoc o )
226     {
227         // check argument
228
if ( o == null || !( o instanceof IValue ) )
229         {
230             return false;
231         }
232         IValue vc = ( IValue ) o;
233
234         // compare attributes
235
if ( !vc.getAttribute().equals( this.getAttribute() ) )
236         {
237             return false;
238         }
239
240         // compare values
241
if ( this.isEmpty() && vc.isEmpty() )
242         {
243             return true;
244         }
245         else if ( this.isBinary() && vc.isBinary() )
246         {
247             return Utils.equals( this.getBinaryValue(), vc.getBinaryValue() );
248         }
249         else if ( this.isString() && vc.isString() )
250         {
251             return ( this.getStringValue().equals( vc.getStringValue() ) );
252         }
253         else
254         {
255             return false;
256         }
257     }
258
259
260     /**
261      * {@inheritDoc}
262      */

263     public int hashCode()
264     {
265         return rawValue.hashCode();
266     }
267
268
269     /**
270      * {@inheritDoc}
271      */

272     public String JavaDoc toString()
273     {
274         return attribute + ":" + ( this.isString() ? this.getStringValue() : "BINARY" ); //$NON-NLS-1$ //$NON-NLS-2$
275
}
276
277
278     /**
279      * {@inheritDoc}
280      */

281     public Object JavaDoc getAdapter( Class JavaDoc adapter )
282     {
283         Class JavaDoc<?> clazz = ( Class JavaDoc<?> ) adapter;
284         if ( clazz.isAssignableFrom( ISearchPageScoreComputer.class ) )
285         {
286             return new LdapSearchPageScoreComputer();
287         }
288         if ( clazz.isAssignableFrom( IConnection.class ) )
289         {
290             return getAttribute().getEntry().getConnection();
291         }
292         if ( clazz.isAssignableFrom( IEntry.class ) )
293         {
294             return getAttribute().getEntry();
295         }
296         if ( clazz.isAssignableFrom( IAttribute.class ) )
297         {
298             return getAttribute();
299         }
300         if ( clazz.isAssignableFrom( IValue.class ) )
301         {
302             return this;
303         }
304         return null;
305     }
306
307
308     /**
309      * {@inheritDoc}
310      */

311     public boolean isRdnPart()
312     {
313         RDNPart[] parts = getAttribute().getEntry().getRdn().getParts();
314         for ( int p = 0; p < parts.length; p++ )
315         {
316             if ( getAttribute().getDescription().equals( parts[p].getType() )
317                 && getStringValue().equals( parts[p].getValue() ) )
318             {
319                 return true;
320             }
321         }
322         return false;
323     }
324
325 }
326
Popular Tags