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