com.hazelcast.internal.util.collection.ImmutableInflatableSet Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.internal.util.collection;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import com.hazelcast.internal.serialization.SerializableByConvention;
/**
* An immutable {@link InflatableSet}.
*
* @param the type of elements maintained by this set
*/
@SerializableByConvention
public final class ImmutableInflatableSet extends InflatableSet {
private static final long serialVersionUID = 1L;
private ImmutableInflatableSet(List compactList) {
super(compactList);
}
public static ImmutableSetBuilder newImmutableSetBuilder(int initialCapacity) {
return new ImmutableSetBuilder(initialCapacity);
}
@Override
public Iterator iterator() {
if (state == State.INFLATED) {
throw new IllegalStateException("Set is immutable and can not be inflated.");
}
return new ImmutableHybridIterator();
}
private final class ImmutableHybridIterator extends HybridIterator {
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
/**
* Builder for {@link ImmutableInflatableSet}.
* This is the only way to create a new instance of ImmutableInflatableSet.
*
* @param the type of elements maintained by this set
*/
public static final class ImmutableSetBuilder extends AbstractBuilder {
private ImmutableSetBuilder(int initialCapacity) {
super(initialCapacity);
}
public ImmutableSetBuilder add(T item) {
super.add(item);
return this;
}
public ImmutableInflatableSet build() {
ImmutableInflatableSet set = new ImmutableInflatableSet<>(list);
// make sure no further insertions are possible
list = Collections.emptyList();
return set;
}
}
@Override
public boolean add(T t) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection extends T> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeIf(Predicate super T> filter) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection> coll) {
throw new UnsupportedOperationException();
}
}