org.osgi.util.converter.SetDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aem-sdk-api Show documentation
Show all versions of aem-sdk-api Show documentation
The Adobe Experience Manager SDK
/*******************************************************************************
* Copyright (c) Contributors to the Eclipse Foundation
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
package org.osgi.util.converter;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* @author $Id: 5d96c2d402f714814dc426d564dc3f03e869976e $
*/
class SetDelegate implements Set {
private volatile Set delegate;
private volatile boolean cloned;
private final ConvertingImpl convertingImpl;
private final InternalConverter converter;
static Set forCollection(Collection collection,
ConvertingImpl converting, InternalConverter c) {
if (collection instanceof Set) {
return new SetDelegate((Set) collection, converting, c);
}
return new SetDelegate(new CollectionSetDelegate<>(collection),
converting, c);
}
SetDelegate(Set collection, ConvertingImpl converting,
InternalConverter c) {
delegate = collection;
convertingImpl = converting;
converter = c;
}
// Whenever a modification is made, the delegate is cloned and detached.
private void cloneDelegate() {
if (cloned) {
return;
} else {
cloned = true;
delegate = new HashSet<>(delegate);
}
}
@Override
public int size() {
return delegate.size();
}
@Override
public boolean isEmpty() {
return delegate.isEmpty();
}
@Override
public boolean contains(Object o) {
return containsAll(Collections.singletonList(o));
}
@Override
public Iterator iterator() {
return new SetDelegateIterator();
}
@Override
public Object[] toArray() {
return toArray(new Object[size()]);
}
@SuppressWarnings("unchecked")
@Override
public X[] toArray(X[] a) {
int mySize = size();
if (Array.getLength(a) < size()) {
a = (X[]) Array.newInstance(a.getClass().getComponentType(),
mySize);
}
Iterator it = iterator();
for (int i = 0; i < a.length; i++) {
if (mySize > i && it.hasNext()) {
a[i] = (X) it.next();
} else {
a[i] = null;
}
}
return a;
}
@Override
public boolean add(T e) {
cloneDelegate();
return delegate.add(e);
}
@Override
public boolean remove(Object o) {
cloneDelegate();
return delegate.remove(o);
}
@Override
public boolean containsAll(Collection< ? > c) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy