ArraySortedSet.java revision 2d7e1111358e2b8cc951a46dc8b0217a7fa0dead
1/* 2 * Copyright 2012, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32package org.jf.util; 33 34import com.google.common.collect.Iterators; 35 36import javax.annotation.Nonnull; 37import java.util.*; 38 39public class ArraySortedSet<T> implements SortedSet<T> { 40 @Nonnull private final Comparator<? super T> comparator; 41 @Nonnull private final Object[] arr; 42 43 private ArraySortedSet(@Nonnull Comparator<? super T> comparator, @Nonnull T[] arr) { 44 // we assume arr is already sorted by comparator, and all entries are unique 45 this.comparator = comparator; 46 this.arr = arr; 47 } 48 49 public static <T> ArraySortedSet<T> of(@Nonnull Comparator<? super T> comparator, @Nonnull T[] arr) { 50 return new ArraySortedSet<T>(comparator, arr); 51 } 52 53 @Override 54 public int size() { 55 return arr.length; 56 } 57 58 @Override 59 public boolean isEmpty() { 60 return arr.length > 0; 61 } 62 63 @Override 64 @SuppressWarnings("unchecked") 65 public boolean contains(Object o) { 66 return Arrays.binarySearch((T[])arr, (T)o, comparator) >= 0; 67 } 68 69 @Override 70 @SuppressWarnings("unchecked") 71 public Iterator<T> iterator() { 72 return Iterators.forArray((T[])arr); 73 } 74 75 @Override 76 public Object[] toArray() { 77 return arr.clone(); 78 } 79 80 @Override 81 @SuppressWarnings("unchecked") 82 public <T> T[] toArray(T[] a) { 83 if (a.length <= arr.length) { 84 System.arraycopy(arr, 0, (Object[])a, 0, arr.length); 85 return a; 86 } 87 return Arrays.copyOf((T[])arr, arr.length); 88 } 89 90 @Override 91 public boolean add(T t) { 92 throw new UnsupportedOperationException(); 93 } 94 95 @Override 96 public boolean remove(Object o) { 97 throw new UnsupportedOperationException(); 98 } 99 100 @Override 101 public boolean containsAll(Collection<?> c) { 102 for (Object o: c) { 103 if (!contains(o)) { 104 return false; 105 } 106 } 107 return true; 108 } 109 110 @Override 111 public boolean addAll(Collection<? extends T> c) { 112 throw new UnsupportedOperationException(); 113 } 114 115 @Override 116 public boolean retainAll(Collection<?> c) { 117 throw new UnsupportedOperationException(); 118 } 119 120 @Override 121 public boolean removeAll(Collection<?> c) { 122 throw new UnsupportedOperationException(); 123 } 124 125 @Override 126 public void clear() { 127 throw new UnsupportedOperationException(); 128 } 129 130 @Override 131 public Comparator<? super T> comparator() { 132 return comparator; 133 } 134 135 @Override 136 public SortedSet<T> subSet(T fromElement, T toElement) { 137 throw new UnsupportedOperationException(); 138 } 139 140 @Override 141 public SortedSet<T> headSet(T toElement) { 142 throw new UnsupportedOperationException(); 143 } 144 145 @Override 146 public SortedSet<T> tailSet(T fromElement) { 147 throw new UnsupportedOperationException(); 148 } 149 150 @Override 151 @SuppressWarnings("unchecked") 152 public T first() { 153 if (arr.length == 0) { 154 throw new NoSuchElementException(); 155 } 156 return (T)arr[0]; 157 } 158 159 @Override 160 @SuppressWarnings("unchecked") 161 public T last() { 162 if (arr.length == 0) { 163 throw new NoSuchElementException(); 164 } 165 return (T)arr[arr.length-1]; 166 } 167 168 @Override 169 public int hashCode() { 170 int result = 0; 171 for (Object o: arr) { 172 result += o.hashCode(); 173 } 174 return result; 175 } 176 177 @Override 178 public boolean equals(Object o) { 179 if (o == null) { 180 return false; 181 } 182 if (o instanceof SortedSet) { 183 SortedSet other = (SortedSet)o; 184 if (arr.length != other.size()) { 185 return false; 186 } 187 return Iterators.elementsEqual(iterator(), other.iterator()); 188 } 189 if (o instanceof Set) { 190 Set other = (Set)o; 191 if (arr.length != other.size()) { 192 return false; 193 } 194 return this.containsAll(other); 195 } 196 return false; 197 } 198} 199