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

org.jamesii.ml3.model.values.SetValue Maven / Gradle / Ivy

Go to download

The Modeling Language for Linked Lives, a domain specific modeling language for agent-based computational demography.

There is a newer version: 0.2.8
Show newest version
/*
 * Copyright 2017 University of Rostock
 *
 * 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.
 */

package org.jamesii.ml3.model.values;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jamesii.ml3.model.types.BasicType;
import org.jamesii.ml3.model.types.IType;
import org.jamesii.ml3.model.types.SetType;

/**
 * {@link IValue}-implementation for list values. Encapsulates a {@link List}.
 * 
 * @author Oliver Reinhardt
 */
public class SetValue implements IValue {
	private final Set value;
	
	public SetValue() {
		this.value = Collections.emptySet();
	}
	
	public  SetValue(Collection value) {
		if (value instanceof Set)
			this.value = Collections.unmodifiableSet((Set) value);
		else
			this.value = Collections.unmodifiableSet(new HashSet(value));
	}

	@Override
	public Set getValue() {
		return value;
	}

	public int size() {
		return value.size();
	}

	@Override
	public IType getType() {
		if (value.isEmpty()) {
			return new SetType(BasicType.UNKNOWN);
		} else {
			return new SetType(value.iterator().next().getType());
		}
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (!(o instanceof SetValue))
			return false;

		SetValue value1 = (SetValue) o;

		return value.equals(value1.value);

	}

	@Override
	public int hashCode() {
		return value.hashCode();
	}
	
	@Override
	public String toString() {
		return value.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy