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

org.sejda.common.collection.NullSafeSet Maven / Gradle / Ivy

There is a newer version: 5.1.7
Show newest version
/*
 * Created on 22/ago/2011
 * Copyright 2011 by Andrea Vacondio ([email protected]).
 * 
 * This file is part of the Sejda source code
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package org.sejda.common.collection;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * {@link LinkedHashSet} wrapper disallowing null elements.
 * 
 * @author Andrea Vacondio
 * @param 
 *            type of elements of the set.
 */
public class NullSafeSet implements Set {

    private Set delegate;

    public NullSafeSet() {
        delegate = new LinkedHashSet<>();
    }

    public NullSafeSet(Set delegate) {
        this.delegate = delegate;
    }

    @Override
    public int size() {
        return delegate.size();
    }

    @Override
    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return delegate.contains(o);
    }

    @Override
    public Iterator iterator() {
        return delegate.iterator();
    }

    @Override
    public Object[] toArray() {
        return delegate.toArray();
    }

    @Override
    public  T[] toArray(T[] a) {
        return delegate.toArray(a);
    }

    /**
     * Adds the input element if it's not null.
     */
    @Override
    public boolean add(E e) {
        if (e != null) {
            return delegate.add(e);
        }
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return delegate.remove(o);
    }

    @Override
    public boolean containsAll(Collection c) {
        return delegate.containsAll(c);
    }

    @Override
    public boolean addAll(Collection c) {
        boolean retVal = false;
        for (E e : c) {
            if (add(e)) {
                retVal = true;
            }
        }
        return retVal;
    }

    @Override
    public boolean retainAll(Collection c) {
        return delegate.retainAll(c);
    }

    @Override
    public boolean removeAll(Collection c) {
        return delegate.removeAll(c);
    }

    @Override
    public void clear() {
        delegate.clear();
    }

    @Override
    public boolean equals(Object o) {
        return delegate.equals(o);
    }

    @Override
    public int hashCode() {
        return delegate.hashCode();
    }

    @Override
    public String toString() {
        return delegate.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy