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.SortedSet; 025 026import org.apache.commons.collections4.Unmodifiable; 027import org.apache.commons.collections4.iterators.UnmodifiableIterator; 028 029/** 030 * Decorates another <code>SortedSet</code> to ensure it can't be altered. 031 * <p> 032 * This class is Serializable from Commons Collections 3.1. 033 * <p> 034 * Attempts to modify it will result in an UnsupportedOperationException. 035 * 036 * @param <E> the type of the elements in this set 037 * @since 3.0 038 */ 039public final class UnmodifiableSortedSet<E> 040 extends AbstractSortedSetDecorator<E> 041 implements Unmodifiable { 042 043 /** Serialization version */ 044 private static final long serialVersionUID = -725356885467962424L; 045 046 /** 047 * Factory method to create an unmodifiable set. 048 * 049 * @param <E> the element type 050 * @param set the set to decorate, must not be null 051 * @return a new unmodifiable {@link SortedSet} 052 * @throws NullPointerException if set is null 053 * @since 4.0 054 */ 055 public static <E> SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set) { 056 if (set instanceof Unmodifiable) { 057 return set; 058 } 059 return new UnmodifiableSortedSet<>(set); 060 } 061 062 //----------------------------------------------------------------------- 063 /** 064 * Constructor that wraps (not copies). 065 * 066 * @param set the set to decorate, must not be null 067 * @throws NullPointerException if set is null 068 */ 069 private UnmodifiableSortedSet(final SortedSet<E> set) { 070 super(set); 071 } 072 073 //----------------------------------------------------------------------- 074 @Override 075 public Iterator<E> iterator() { 076 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator()); 077 } 078 079 @Override 080 public boolean add(final E object) { 081 throw new UnsupportedOperationException(); 082 } 083 084 @Override 085 public boolean addAll(final Collection<? extends E> coll) { 086 throw new UnsupportedOperationException(); 087 } 088 089 @Override 090 public void clear() { 091 throw new UnsupportedOperationException(); 092 } 093 094 @Override 095 public boolean remove(final Object object) { 096 throw new UnsupportedOperationException(); 097 } 098 099 @Override 100 public boolean removeAll(final Collection<?> coll) { 101 throw new UnsupportedOperationException(); 102 } 103 104 @Override 105 public boolean retainAll(final Collection<?> coll) { 106 throw new UnsupportedOperationException(); 107 } 108 109 //----------------------------------------------------------------------- 110 @Override 111 public SortedSet<E> subSet(final E fromElement, final E toElement) { 112 final SortedSet<E> sub = decorated().subSet(fromElement, toElement); 113 return unmodifiableSortedSet(sub); 114 } 115 116 @Override 117 public SortedSet<E> headSet(final E toElement) { 118 final SortedSet<E> head = decorated().headSet(toElement); 119 return unmodifiableSortedSet(head); 120 } 121 122 @Override 123 public SortedSet<E> tailSet(final E fromElement) { 124 final SortedSet<E> tail = decorated().tailSet(fromElement); 125 return unmodifiableSortedSet(tail); 126 } 127 128 //----------------------------------------------------------------------- 129 /** 130 * Write the collection out using a custom routine. 131 * 132 * @param out the output stream 133 * @throws IOException if an error occurs while writing to the stream 134 */ 135 private void writeObject(final ObjectOutputStream out) throws IOException { 136 out.defaultWriteObject(); 137 out.writeObject(decorated()); 138 } 139 140 /** 141 * Read the collection in using a custom routine. 142 * 143 * @param in the input stream 144 * @throws IOException if an error occurs while reading from the stream 145 * @throws ClassNotFoundException if an object read from the stream can not be loaded 146 */ 147 @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect 148 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 149 in.defaultReadObject(); 150 setCollection((Collection<E>) in.readObject()); // (1) 151 } 152 153}