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

com.liferay.portal.kernel.util.MapBackedSet Maven / Gradle / Ivy

Go to download

Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.

There is a newer version: 7.0.0-nightly
Show newest version
/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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 Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author Shuyang Zhou
 */
public class MapBackedSet extends AbstractSet implements Serializable {

	public MapBackedSet(Map backedMap) {
		if (!backedMap.isEmpty()) {
			throw new IllegalArgumentException("Map is not empty");
		}

		_backedMap = backedMap;
		_backedMapKeySet = backedMap.keySet();
	}

	@Override
	public boolean add(E element) {
		if (_backedMap.put(element, Boolean.TRUE) == null) {
			return true;
		}
		else {
			return false;
		}
	}

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

	@Override
	public boolean contains(Object obj) {
		return _backedMap.containsKey(obj);
	}

	@Override
	public boolean containsAll(Collection collection) {
		return _backedMapKeySet.containsAll(collection);
	}

	@Override
	public boolean equals(Object obj) {
		if ((obj == this) || _backedMapKeySet.equals(obj)) {
			return true;
		}
		else {
			return false;
		}
	}

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

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

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

	@Override
	public boolean remove(Object obj) {
		if (_backedMap.remove(obj) != null) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public boolean removeAll(Collection collection) {
		return _backedMapKeySet.removeAll(collection);
	}

	@Override
	public boolean retainAll(Collection collection) {
		return _backedMapKeySet.retainAll(collection);
	}

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

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

	@Override
	public  T[] toArray(T[] array) {
		return _backedMapKeySet.toArray(array);
	}

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

	private void readObject(ObjectInputStream objectInputStream)
		throws ClassNotFoundException, IOException {

		objectInputStream.defaultReadObject();

		_backedMapKeySet = _backedMap.keySet();
	}

	private final Map _backedMap;
	private transient Set _backedMapKeySet;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy