Java tutorial
/* * (c) 2005 David B. Bracewell * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF 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 com.davidbracewell.math.linear; import com.davidbracewell.math.DoubleEntry; import com.davidbracewell.tuple.Pair; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.collect.Iterators; import com.google.common.primitives.Doubles; import javax.annotation.Nullable; import java.util.*; /** * The type Vector map. * * @author David B. Bracewell */ public class VectorMap extends AbstractMap<Integer, Double> { private final Vector vector; private final Predicate<Double> isValidValue; /** * Instantiates a new Vector map. * * @param vector the vector * @param validValue the valid value */ protected VectorMap(Vector vector, Predicate<Double> validValue) { this.vector = vector; isValidValue = validValue; } /** * Wrap map. * * @param v the v * @param validValue the valid value * @return the map */ public static Map<Integer, Double> wrap(Vector v, Predicate<Double> validValue) { return new VectorMap(Preconditions.checkNotNull(v), validValue); } /** * Wrap map. * * @param v the v * @return the map */ public static Map<Integer, Double> wrap(Vector v) { return new VectorMap(Preconditions.checkNotNull(v), VALID_VALUES.NON_ZERO); } @Override public int size() { return vector.dimension(); } @Override public boolean containsKey(Object key) { return key instanceof Number && isValidValue.apply(vector.get(((Number) key).intValue())); } @Override public Double get(Object key) { if (key instanceof Number) { return vector.get(((Number) key).intValue()); } return 0d; } @Override public Set<Integer> keySet() { return new AbstractSet<Integer>() { @Override public Iterator<Integer> iterator() { return Iterators.transform(vector.nonZeroIterator(), new Function<DoubleEntry, Integer>() { @Nullable @Override public Integer apply(@Nullable DoubleEntry input) { return input == null ? null : input.index; } }); } @Override public int size() { return vector.size(); } }; } @Override public Set<Entry<Integer, Double>> entrySet() { return new AbstractSet<Entry<Integer, Double>>() { @Override public Iterator<Entry<Integer, Double>> iterator() { return Iterators.transform(vector.iterator(), new Function<DoubleEntry, Entry<Integer, Double>>() { @Nullable @Override public Entry<Integer, Double> apply(@Nullable DoubleEntry input) { return input == null ? null : Pair.of(input.index, input.value); } }); } @Override public int size() { return vector.dimension(); } }; } public static enum VALID_VALUES implements Predicate<Double> { /** * The NON_ZERO. */ NON_ZERO { @Override public boolean apply(@Nullable Double input) { return input != null && input != 0; } }, /** * The FINITE. */ FINITE { @Override public boolean apply(@Nullable Double input) { return input != null && Doubles.isFinite(input); } } } }//END OF VectorMap