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 legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2008 Sun Microsystems, Inc. 025 * Portions Copyright 2014 ForgeRock AS 026 */ 027package org.opends.server.types; 028 029/** 030 * This class defines a data structure that defines a set of sort 031 * criteria that may be used to order entries in a set of search 032 * results. The sort order object is comprised of one or more sort 033 * keys, which indicate which attribute types should be used to 034 * perform the sort and information about the ordering to use for 035 * those attributes. If the sort order has multiple sort keys, then 036 * the first sort key will be used as the primary sort criteria, and 037 * the second will only be used in cases where the values of the 038 * attribute associated with the first sort key are equal, the third 039 * will only be used if the first and second values are equal, etc. 040 * If all of the sort key attributes for two entries are identical, 041 * then the relative order for those entries is undefined. 042 * <p> 043 * FIXME: replace with the SDK's SortKey? 044 */ 045@org.opends.server.types.PublicAPI( 046 stability=org.opends.server.types.StabilityLevel.VOLATILE, 047 mayInstantiate=true, 048 mayExtend=false, 049 mayInvoke=true) 050public final class SortOrder 051{ 052 /** The set of sort keys in this sort order. */ 053 private SortKey[] sortKeys; 054 055 /** 056 * Creates a new sort order with a single key. 057 * 058 * @param sortKey The sort key to use in this sort order. 059 */ 060 public SortOrder(SortKey sortKey) 061 { 062 this.sortKeys = new SortKey[] { sortKey }; 063 } 064 065 066 067 /** 068 * Creates a new sort order with the provided set of sort keys. 069 * 070 * @param sortKeys The set of sort keys to use for this sort 071 * order. 072 */ 073 public SortOrder(SortKey... sortKeys) 074 { 075 this.sortKeys = new SortKey[sortKeys.length]; 076 System.arraycopy(sortKeys, 0, this.sortKeys, 0, sortKeys.length); 077 } 078 079 080 081 /** 082 * Retrieves the sort keys for this sort order. 083 * 084 * @return The sort keys for this sort order. 085 */ 086 public SortKey[] getSortKeys() 087 { 088 return sortKeys; 089 } 090 091 092 093 /** 094 * Retrieves a string representation of this sort order. 095 * 096 * @return A string representation of this sort order. 097 */ 098 @Override 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 @Override 139 public int hashCode() 140 { 141 int hashCode = 0; 142 for(SortKey sortKey : sortKeys) 143 { 144 hashCode += sortKey.hashCode(); 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 @Override 159 public boolean equals(Object o) 160 { 161 if(o == null) 162 { 163 return false; 164 } 165 if (o == this) 166 { 167 return true; 168 } 169 if (! (o instanceof SortOrder)) 170 { 171 return false; 172 } 173 174 SortOrder s = (SortOrder) o; 175 if(sortKeys.length != s.sortKeys.length) 176 { 177 return false; 178 } 179 180 for(int i = 0; i < sortKeys.length; i++) 181 { 182 if(!sortKeys[i].equals(s.sortKeys[i])) 183 { 184 return false; 185 } 186 } 187 return true; 188 } 189} 190