/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 org.apache.gossip.crdt; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; public class GrowOnlySet implements CrdtSet, GrowOnlySet>{ private final Set hidden = new LinkedHashSet<>(); @SuppressWarnings("unused") /* * Used by SerDe */ private GrowOnlySet(){ } public GrowOnlySet(Set c){ hidden.addAll(c); } public GrowOnlySet(Collection c){ hidden.addAll(c); } public GrowOnlySet(GrowOnlySet first, GrowOnlySet second){ hidden.addAll(first.value()); hidden.addAll(second.value()); } @Override public GrowOnlySet merge(GrowOnlySet other) { return new GrowOnlySet<>(this, other); } @Override public Set value() { Set copy = new LinkedHashSet<>(); copy.addAll(hidden); return Collections.unmodifiableSet(copy); } @Override public GrowOnlySet optimize() { return new GrowOnlySet<>(hidden); } @Override public int size() { return hidden.size(); } @Override public boolean isEmpty() { return hidden.isEmpty(); } @Override public boolean contains(Object o) { return hidden.contains(o); } @Override public Iterator iterator() { Set copy = new HashSet<>(); copy.addAll(hidden); return copy.iterator(); } @Override public Object[] toArray() { return hidden.toArray(); } @Override public T[] toArray(T[] a) { return hidden.toArray(a); } @Override public boolean add(ElementType e) { throw new UnsupportedOperationException(); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean containsAll(Collection c) { return hidden.containsAll(c); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public String toString() { return "GrowOnlySet [hidden=" + hidden + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((hidden == null) ? 0 : hidden.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; @SuppressWarnings("rawtypes") GrowOnlySet other = (GrowOnlySet) obj; if (hidden == null) { if (other.hidden != null) return false; } else if (!hidden.equals(other.hidden)) return false; return true; } }