KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Comparator JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
27 import org.apache.directory.ldapstudio.browser.core.model.DN;
28 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
29 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
30 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
31 import org.apache.directory.ldapstudio.browser.core.model.IValue;
32 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
33 import org.apache.directory.ldapstudio.browser.core.utils.ModelConverter;
34
35
36 public class AttributeComparator implements Comparator JavaDoc
37 {
38
39     private IEntry dummyEntry;
40
41
42     public AttributeComparator( IConnection connection )
43     {
44         this.dummyEntry = new DummyEntry( new DN(), connection );
45     }
46
47
48     public AttributeComparator( IEntry entry )
49     {
50         this.dummyEntry = entry;
51     }
52
53
54     public int compare( Object JavaDoc o1, Object JavaDoc o2 )
55     {
56
57         IAttribute attribute1 = null;
58         IValue value1 = null;
59         if ( o1 instanceof IAttribute )
60         {
61             attribute1 = ( IAttribute ) o1;
62         }
63         else if ( o1 instanceof IValue )
64         {
65             value1 = ( IValue ) o1;
66             attribute1 = value1.getAttribute();
67         }
68         else if ( o1 instanceof LdifAttrValLine )
69         {
70             LdifAttrValLine line1 = ( LdifAttrValLine ) o1;
71             value1 = ModelConverter.ldifAttrValLineToValue( line1, dummyEntry );
72             attribute1 = value1.getAttribute();
73         }
74
75         IAttribute attribute2 = null;
76         IValue value2 = null;
77         if ( o2 instanceof IAttribute )
78         {
79             attribute2 = ( IAttribute ) o2;
80         }
81         else if ( o2 instanceof IValue )
82         {
83             value2 = ( IValue ) o2;
84             attribute2 = value2.getAttribute();
85         }
86         else if ( o2 instanceof LdifAttrValLine )
87         {
88             LdifAttrValLine line2 = ( LdifAttrValLine ) o2;
89             value2 = ModelConverter.ldifAttrValLineToValue( line2, dummyEntry );
90             attribute2 = value2.getAttribute();
91         }
92
93         if ( value1 != null && value2 != null )
94         {
95             if ( this.getSortByOrDefault() == BrowserCoreConstants.SORT_BY_ATTRIBUTE_DESCRIPTION )
96             {
97                 if ( value1.getAttribute() != value2.getAttribute() )
98                 {
99                     return this.compareAttributeNames( value1.getAttribute(), value2.getAttribute() );
100                 }
101                 else
102                 {
103                     return this.compareValues( value1, value2 );
104                 }
105             }
106             else if ( this.getSortByOrDefault() == BrowserCoreConstants.SORT_BY_VALUE )
107             {
108                 return this.compareValues( value1, value2 );
109             }
110             else
111             {
112                 return this.equal();
113             }
114         }
115         else if ( attribute1 != null && attribute2 != null )
116         {
117             return this.compareAttributeNames( attribute1, attribute2 );
118         }
119         else
120         {
121             return this.equal();
122         }
123     }
124
125
126     private int compareAttributeNames( IAttribute attribute1, IAttribute attribute2 )
127     {
128
129         if ( attribute1.isObjectClassAttribute() )
130         {
131             return lessThan();
132         }
133         else if ( attribute2.isObjectClassAttribute() )
134         {
135             return greaterThan();
136         }
137
138         if ( attribute1.isMustAttribute() && !attribute2.isMustAttribute() )
139         {
140             return lessThan();
141         }
142         else if ( attribute2.isMustAttribute() && !attribute1.isMustAttribute() )
143         {
144             return greaterThan();
145         }
146
147         if ( attribute1.isOperationalAttribute() && !attribute2.isOperationalAttribute() )
148         {
149             return greaterThan();
150         }
151         else if ( attribute2.isOperationalAttribute() && !attribute1.isOperationalAttribute() )
152         {
153             return lessThan();
154         }
155
156         return compare( attribute1.getDescription(), attribute2.getDescription() );
157     }
158
159
160     private int compareValues( IValue value1, IValue value2 )
161     {
162
163         if ( value1.isEmpty() && value2.isEmpty() )
164         {
165             return equal();
166         }
167
168         if ( value1.isEmpty() && !value2.isEmpty() )
169         {
170             return greaterThan();
171         }
172         if ( !value1.isEmpty() && value2.isEmpty() )
173         {
174             return lessThan();
175         }
176
177         return compare( value1.getStringValue(), value2.getStringValue() );
178     }
179
180
181     private int getSortOrderOrDefault()
182     {
183         return BrowserCoreConstants.SORT_ORDER_ASCENDING;
184     }
185
186
187     private int getSortByOrDefault()
188     {
189         return BrowserCoreConstants.SORT_BY_ATTRIBUTE_DESCRIPTION;
190     }
191
192
193     private int lessThan()
194     {
195         return this.getSortOrderOrDefault() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? -1 : 1;
196     }
197
198
199     private int equal()
200     {
201         return 0;
202     }
203
204
205     private int greaterThan()
206     {
207         return this.getSortOrderOrDefault() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? 1 : -1;
208     }
209
210
211     private int compare( String JavaDoc s1, String JavaDoc s2 )
212     {
213         return this.getSortOrderOrDefault() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? s1.compareToIgnoreCase( s2 )
214             : s2.compareToIgnoreCase( s1 );
215     }
216
217 }
218
Popular Tags