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

org.jamesii.ml3.model.maps.ContinuousValueMap 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.maps;

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
import java.util.TreeMap;

import org.jamesii.ml3.model.types.BasicType;
import org.jamesii.ml3.model.types.IType;
import org.jamesii.ml3.model.types.TypeUtil;
import org.jamesii.ml3.model.values.INumericalValue;
import org.jamesii.ml3.model.values.IValue;

/**
 * A {@link IValueMap} implementation for maps with continuous parameter space.
 * These maps return the value of the largest key lower or equal to the given
 * key.
 * 
 * @author Oliver Reinhardt
 */
public class ContinuousValueMap implements IValueMap {
	private NavigableMap map;
	private IValue defaultValue;
	
	public ContinuousValueMap(IValue defaultValue) {
		this.map = new TreeMap<>();
		this.defaultValue = defaultValue;
	}

	@Override
	public void put(IValue key, IValue value) {
		map.put(((INumericalValue) key).getDouble(), value);
	}

	@Override
	public IValue get(IValue key) {
		if (key instanceof INumericalValue) {
			INumericalValue rkey = (INumericalValue) key;
			if (!map.isEmpty() && rkey.getDouble() >= map.firstKey())
				return map.floorEntry(rkey.getDouble()).getValue();
			else
				return defaultValue;
		} else
			return null;
	}

	@Override
	public IType getValueType() {
		return TypeUtil.getType(getValues());
	}

	@Override
	public IType getKeyType() {
		return BasicType.REAL;
	}


	public List getBorders() {
		ArrayList keys = new ArrayList();
		keys.addAll(map.navigableKeySet());
		return keys;
	}

	public List getBorders(double offset) {
		ArrayList keys = new ArrayList();
		for (double cp : map.navigableKeySet()) {
			keys.add(cp + offset);
		}
		return keys;
	}

	public List getValues() {
		ArrayList values = new ArrayList();
		values.add(defaultValue);
		values.addAll(map.values());
		return values;
	}

		public String toString() {
		
		return map.toString();
	}
	
	public void setDefault(IValue v) {
		this.defaultValue = v;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy