001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.types;
028
029
030 /**
031 * This class defines a data structure that defines a set of sort
032 * criteria that may be used to order entries in a set of search
033 * results. The sort order object is comprised of one or more sort
034 * keys, which indicate which attribute types should be used to
035 * perform the sort and information about the ordering to use for
036 * those attributes. If the sort order has multiple sort keys, then
037 * the first sort key will be used as the primary sort criteria, and
038 * the second will only be used in cases where the values of the
039 * attribute associated with the first sort key are equal, the third
040 * will only be used if the first and second values are equal, etc.
041 * If all of the sort key attributes for two entries are identical,
042 * then the relative order for those entries is undefined.
043 */
044 @org.opends.server.types.PublicAPI(
045 stability=org.opends.server.types.StabilityLevel.VOLATILE,
046 mayInstantiate=true,
047 mayExtend=false,
048 mayInvoke=true)
049 public final class SortOrder
050 {
051 // The set of sort keys in this sort order.
052 private SortKey[] sortKeys;
053
054
055
056 /**
057 * Creates a new sort order with a single key.
058 *
059 * @param sortKey The sort key to use in this sort order.
060 */
061 public SortOrder(SortKey sortKey)
062 {
063 this.sortKeys = new SortKey[] { sortKey };
064 }
065
066
067
068 /**
069 * Creates a new sort order with the provided set of sort keys.
070 *
071 * @param sortKeys The set of sort keys to use for this sort
072 * order.
073 */
074 public SortOrder(SortKey[] sortKeys)
075 {
076 this.sortKeys = new SortKey[sortKeys.length];
077 System.arraycopy(sortKeys, 0, this.sortKeys, 0, sortKeys.length);
078 }
079
080
081
082 /**
083 * Retrieves the sort keys for this sort order.
084 *
085 * @return The sort keys for this sort order.
086 */
087 public SortKey[] getSortKeys()
088 {
089 return sortKeys;
090 }
091
092
093
094 /**
095 * Retrieves a string representation of this sort order.
096 *
097 * @return A string representation of this sort order.
098 */
099 public String toString()
100 {
101 StringBuilder buffer = new StringBuilder();
102 toString(buffer);
103 return buffer.toString();
104 }
105
106
107
108 /**
109 * Appends a string representation of this sort order to the
110 * provided buffer.
111 *
112 * @param buffer The buffer to which the information should be
113 * appended.
114 */
115 public void toString(StringBuilder buffer)
116 {
117 buffer.append("SortOrder(");
118
119 if (sortKeys.length > 0)
120 {
121 sortKeys[0].toString(buffer);
122
123 for (int i=1; i < sortKeys.length; i++)
124 {
125 buffer.append(",");
126 sortKeys[i].toString(buffer);
127 }
128 }
129
130 buffer.append(")");
131 }
132
133 /**
134 * Retrieves the hash code for this sort order.
135 *
136 * @return The hash code for this sort order.
137 */
138 public int hashCode()
139 {
140 int hashCode = 0;
141 for(SortKey sortKey : sortKeys)
142 {
143 hashCode += sortKey.hashCode();
144 }
145
146 return hashCode;
147 }
148
149 /**
150 * Indicates whether this sort order is equal to the provided
151 * object.
152 *
153 * @param o The object for which to make the determination.
154 *
155 * @return <CODE>true</CODE> if the provide object is equal to this
156 * sort order, or <CODE>false</CODE> if it is not.
157 */
158 public boolean equals(Object o)
159 {
160 if(o == null)
161 {
162 return false;
163 }
164
165 if (o == this)
166 {
167 return true;
168 }
169
170 if (! (o instanceof SortOrder))
171 {
172 return false;
173 }
174
175 SortOrder s = (SortOrder) o;
176
177 if(sortKeys.length != s.sortKeys.length)
178 {
179 return false;
180 }
181
182 for(int i = 0; i < sortKeys.length; i++)
183 {
184 if(!sortKeys[i].equals(s.sortKeys[i]))
185 {
186 return false;
187 }
188 }
189
190 return true;
191 }
192 }
193