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

net.anwiba.commons.lang.tree.TreeItemChooser Maven / Gradle / Ivy

There is a newer version: 1.2.50
Show newest version
/*
 * #%L
 * anwiba commons core
 * %%
 * Copyright (C) 2007 - 2016 Andreas Bartels
 * %%
 * This program 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 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package net.anwiba.commons.lang.tree;

import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import net.anwiba.commons.lang.object.ObjectUtilities;
import net.anwiba.commons.lang.tree.distance.IObjectDistanceCalculator;

public final class TreeItemChooser implements ITreeItemChooser {

  public class ObjectPair {

    private final int hashCode;
    private final T firstObject;
    private final U secondObject;

    public ObjectPair(final T firstObject, final U secondObject) {
      this.firstObject = firstObject;
      this.secondObject = secondObject;
      this.hashCode = calculate();
    }

    public int calculate() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((this.firstObject == null) ? 0 : this.firstObject.hashCode());
      result = prime * result + ((this.secondObject == null) ? 0 : this.secondObject.hashCode());
      return result;
    }

    public final T getFirstObject() {
      return this.firstObject;
    }

    public final U getSecondObject() {
      return this.secondObject;
    }

    @Override
    public boolean equals(final Object obj) {
      if (!(obj instanceof ObjectPair)) {
        return false;
      }
      @SuppressWarnings("unchecked")
      final ObjectPair other = (ObjectPair) obj;
      return ObjectUtilities.equals(this.firstObject, other.firstObject)
          && ObjectUtilities.equals(this.secondObject, other.secondObject);
    }

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

  }

  private final IObjectDistanceCalculator distanceCalculator;
  private final Map, Double> distances = new HashMap<>();
  private final Map>> distanceKeys = new HashMap<>();

  public TreeItemChooser(final IObjectDistanceCalculator distanceCalculator) {
    this.distanceCalculator = distanceCalculator;
  }

  @Override
  public K choose(final Comparator comparator, final ITreeItem item, final K key, final V element) {
    final K itemKey = item.getKey();
    final double distancePrecursorSuccessor = calculate(item.getPrevious().getKey(), item.getNext().getKey());
    if (Double.isNaN(distancePrecursorSuccessor)) {
      return itemKey;
    }
    final double distancePrecursorItem = calculate(item.getPrevious().getKey(), itemKey);
    if (Double.isNaN(distancePrecursorItem)) {
      return key;
    }
    final double distancePrecursorOther = calculate(item.getPrevious().getKey(), key);
    if (Double.isNaN(distancePrecursorOther)) {
      return itemKey;
    }
    if (isOtherMoreCentral(distancePrecursorSuccessor, distancePrecursorItem, distancePrecursorOther)) {
      return key;
    }
    return itemKey;
  }

  private boolean isOtherMoreCentral(final double distance, final double distanceToItem, final double distanceToOther) {
    return Math.abs((distance / 2.) - distanceToOther) < Math.abs((distance / 2.) - distanceToItem);
  }

  private double calculate(final K key, final K other) {
    final ObjectPair distanceKey = new ObjectPair<>(key, other);
    if (this.distances.containsKey(distanceKey)) {
      return this.distances.get(distanceKey).doubleValue();
    }
    final double distance = this.distanceCalculator.calculate(key, other);
    cache(distanceKey, distance);
    return distance;
  }

  private void cache(final ObjectPair distanceKey, final double distance) {
    this.distances.put(distanceKey, Double.valueOf(distance));
    cache(distanceKey.getFirstObject(), distanceKey);
    cache(distanceKey.getSecondObject(), distanceKey);
  }

  private void cache(final K key, final ObjectPair distanceKey) {
    if (!this.distanceKeys.containsKey(key)) {
      this.distanceKeys.put(key, new HashSet>());
    }
    this.distanceKeys.get(key).add(distanceKey);
  }

  @Override
  public void removed(final K key) {
    if (!this.distanceKeys.containsKey(key)) {
      return;
    }
    final Set> set = this.distanceKeys.get(key);
    for (final ObjectPair distanceKey : set) {
      this.distances.remove(distanceKey);
    }
    this.distanceKeys.remove(key);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy