/** * Adds a new distance functor for the given attribute data type. * * @param type the attribute data type * @param d the distance functor to use on the data type */ public static final void putDistanceFunctorFor(String type, Distance d) { Map<String, Distance> fs = distanceMap.get(type); if (fs == null) { fs = new HashMap<String, Distance>(); } fs.put(d.toString(), d); distanceMap.put(type, fs); }
/** * Tests to see if the specified metric name is known for the given attribute data type. * * @param type the attribute type * @param s the name of the distance metric to look for * @return true if <code>s</code> is the name of a known metric for the type * @throws UnknownDistanceException if the type is unknown. XXX: shouldn't this be the other way * around? */ public static final boolean isDistanceFor(String type, String s) throws UnknownDistanceException { check(type); Map<String, Distance> values = distanceMap.get(type); if (values != null) { return values.containsKey(s.toLowerCase()); } else { throw new UnknownDistanceException("Attribute type not found: " + type); } }
/** * Tests to see if the specified metric name is known for the given attribute data type. * * @param type the attribute type * @param s the name of the distance metric to look for * @return true if <code>s</code> is the name of a known metric for the type * @throws UnknownDistanceException if the type is unknown. XXX: shouldn't this be the other way * around? */ public static final boolean isDistanceFor(Measurable type, String s) throws UnknownDistanceException { Map<String, Distance> values = distanceMap.get(type.getType()); if (values != null) { return values.containsKey(s.toLowerCase()); } else { throw new UnknownDistanceException(type, s); } }
/** * Copies all the distance functors from <code>from</code> attribute type to <code>to</code> * attribute type. * * @param to the attribute type to get the copies * @param from the attribute type to receive the distance functor references */ public static final void useSameDistances(String to, String from) { check(from); Map<String, Distance> fromValues = distanceMap.get(from); Map<String, Distance> toValues = distanceMap.get(to); if (fromValues != null) { if (toValues != null) { toValues.putAll(fromValues); } else { toValues = new HashMap<String, Distance>(); toValues.putAll(fromValues); distanceMap.put(to, toValues); } } }
/** * Gets the distance functor for the given type. * * @param type the attribute data type * @param metric the metric name * @return the metric * @throws UnknownDistanceException if the metric isn't found */ public static final Distance getDistanceFunctor(String type, String metric) throws UnknownDistanceException { check(type); metric = metric.toLowerCase(); if (!distanceMap.containsKey(type)) { throw new UnknownDistanceException("Attribute type not found: " + type); } else { Map<String, Distance> distances = distanceMap.get(type); if (!distances.containsKey(metric)) { throw new UnknownDistanceException(type, metric); } else { return (Distance) distances.get(metric); } } }