All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.shibboleth.utilities.java.support.collection.LazySet Maven / Gradle / Ivy

There is a newer version: 8.0.0
Show newest version
/*
 * Licensed to the University Corporation for Advanced Internet Development,
 * Inc. (UCAID) under one or more contributor license agreements.  See the
 * NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.collection;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.annotation.concurrent.NotThreadSafe;

/**
 * A set that is lazy initialized. This set takes very little memory when storing zero or one item.
 * 
 * @param  type of the elements within the set
 */
@NotThreadSafe
public class LazySet implements Set, Serializable {

    /** Serial version UID. */
    private static final long serialVersionUID = -1596445680460115174L;

    /** The delegate set. */
    private Set delegate = Collections.emptySet();

    /** {@inheritDoc} */
    public boolean add(final ElementType element) {
        if (delegate.isEmpty()) {
            delegate = Collections.singleton(element);
            return true;
        } else {
            delegate = createImplementation();
            return delegate.add(element);
        }
    }

    /** {@inheritDoc} */
    public boolean addAll(final Collection collection) {
        delegate = createImplementation();
        return delegate.addAll(collection);
    }

    /** {@inheritDoc} */
    public void clear() {
        delegate = Collections.emptySet();
    }

    /** {@inheritDoc} */
    public boolean contains(final Object element) {
        return delegate.contains(element);
    }

    /** {@inheritDoc} */
    public boolean containsAll(final Collection collection) {
        return delegate.containsAll(collection);
    }

    /** {@inheritDoc} */
    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    /** {@inheritDoc} */
    public Iterator iterator() {
        delegate = createImplementation();
        return delegate.iterator();
    }

    /** {@inheritDoc} */
    public boolean remove(final Object element) {
        delegate = createImplementation();
        return delegate.remove(element);
    }

    /** {@inheritDoc} */
    public boolean removeAll(final Collection collection) {
        delegate = createImplementation();
        return delegate.removeAll(collection);
    }

    /** {@inheritDoc} */
    public boolean retainAll(final Collection collection) {
        delegate = createImplementation();
        return delegate.retainAll(collection);
    }

    /** {@inheritDoc} */
    public int size() {
        return delegate.size();
    }

    /** {@inheritDoc} */
    public Object[] toArray() {
        return delegate.toArray();
    }

    /** {@inheritDoc} */
    public  T[] toArray(final T[] type) {
        return delegate.toArray(type);
    }

    /**
     * Builds an appropriate delegate set.
     * 
     * @return the delegate set
     */
    private Set createImplementation() {
        if (delegate instanceof HashSet) {
            return delegate;
        }

        return new HashSet<>(delegate);
    }

    /** {@inheritDoc} */
    public String toString() {
        return delegate.toString();
    }

    /** {@inheritDoc} */
    public int hashCode() {
        return delegate.hashCode();
    }

    /** {@inheritDoc} */
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || this.getClass() != obj.getClass()) {
            return false;
        }

        return delegate.equals(((LazySet) obj).delegate);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy