001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.set; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.ObjectOutputStream; 022import java.util.Collection; 023import java.util.Iterator; 024import java.util.NavigableSet; 025import java.util.SortedSet; 026 027import org.apache.commons.collections4.Unmodifiable; 028import org.apache.commons.collections4.iterators.UnmodifiableIterator; 029 030/** 031 * Decorates another <code>NavigableSet</code> to ensure it can't be altered. 032 * <p> 033 * Attempts to modify it will result in an UnsupportedOperationException. 034 * 035 * @param <E> the type of the elements in this set 036 * @since 4.1 037 */ 038public final class UnmodifiableNavigableSet<E> 039 extends AbstractNavigableSetDecorator<E> 040 implements Unmodifiable { 041 042 /** Serialization version */ 043 private static final long serialVersionUID = 20150528L; 044 045 /** 046 * Factory method to create an unmodifiable set. 047 * 048 * @param <E> the element type 049 * @param set the set to decorate, must not be null 050 * @return a new unmodifiable {@link NavigableSet} 051 * @throws NullPointerException if set is null 052 */ 053 public static <E> NavigableSet<E> unmodifiableNavigableSet(final NavigableSet<E> set) { 054 if (set instanceof Unmodifiable) { 055 return set; 056 } 057 return new UnmodifiableNavigableSet<>(set); 058 } 059 060 //----------------------------------------------------------------------- 061 /** 062 * Constructor that wraps (not copies). 063 * 064 * @param set the set to decorate, must not be null 065 * @throws NullPointerException if set is null 066 */ 067 private UnmodifiableNavigableSet(final NavigableSet<E> set) { 068 super(set); 069 } 070 071 //----------------------------------------------------------------------- 072 @Override 073 public Iterator<E> iterator() { 074 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator()); 075 } 076 077 @Override 078 public boolean add(final E object) { 079 throw new UnsupportedOperationException(); 080 } 081 082 @Override 083 public boolean addAll(final Collection<? extends E> coll) { 084 throw new UnsupportedOperationException(); 085 } 086 087 @Override 088 public void clear() { 089 throw new UnsupportedOperationException(); 090 } 091 092 @Override 093 public boolean remove(final Object object) { 094 throw new UnsupportedOperationException(); 095 } 096 097 @Override 098 public boolean removeAll(final Collection<?> coll) { 099 throw new UnsupportedOperationException(); 100 } 101 102 @Override 103 public boolean retainAll(final Collection<?> coll) { 104 throw new UnsupportedOperationException(); 105 } 106 107 // SortedSet 108 //----------------------------------------------------------------------- 109 @Override 110 public SortedSet<E> subSet(final E fromElement, final E toElement) { 111 final SortedSet<E> sub = decorated().subSet(fromElement, toElement); 112 return UnmodifiableSortedSet.unmodifiableSortedSet(sub); 113 } 114 115 @Override 116 public SortedSet<E> headSet(final E toElement) { 117 final SortedSet<E> head = decorated().headSet(toElement); 118 return UnmodifiableSortedSet.unmodifiableSortedSet(head); 119 } 120 121 @Override 122 public SortedSet<E> tailSet(final E fromElement) { 123 final SortedSet<E> tail = decorated().tailSet(fromElement); 124 return UnmodifiableSortedSet.unmodifiableSortedSet(tail); 125 } 126 127 // NavigableSet 128 //----------------------------------------------------------------------- 129 @Override 130 public NavigableSet<E> descendingSet() { 131 return unmodifiableNavigableSet(decorated().descendingSet()); 132 } 133 134 @Override 135 public Iterator<E> descendingIterator() { 136 return UnmodifiableIterator.unmodifiableIterator(decorated().descendingIterator()); 137 } 138 139 @Override 140 public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement, 141 final boolean toInclusive) { 142 final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive); 143 return unmodifiableNavigableSet(sub); 144 } 145 146 @Override 147 public NavigableSet<E> headSet(final E toElement, final boolean inclusive) { 148 final NavigableSet<E> head = decorated().headSet(toElement, inclusive); 149 return unmodifiableNavigableSet(head); 150 } 151 152 @Override 153 public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) { 154 final NavigableSet<E> tail = decorated().tailSet(fromElement, inclusive); 155 return unmodifiableNavigableSet(tail); 156 } 157 158 //----------------------------------------------------------------------- 159 /** 160 * Write the collection out using a custom routine. 161 * 162 * @param out the output stream 163 * @throws IOException if an error occurs while writing to the stream 164 */ 165 private void writeObject(final ObjectOutputStream out) throws IOException { 166 out.defaultWriteObject(); 167 out.writeObject(decorated()); 168 } 169 170 /** 171 * Read the collection in using a custom routine. 172 * 173 * @param in the input stream 174 * @throws IOException if an error occurs while reading from the stream 175 * @throws ClassNotFoundException if an object read from the stream can not be loaded 176 */ 177 @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect 178 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 179 in.defaultReadObject(); 180 setCollection((Collection<E>) in.readObject()); // (1) 181 } 182 183}