EncodedValueOld.java

  1. package org.heigit.ors.routing.graphhopper.extensions.flagencoders;

  2. /**
  3.  * @deprecated replace all occurences of EncodedValueOld by regular EncodedValues
  4.  */
  5. @Deprecated
  6. public class EncodedValueOld {
  7.     protected final long shift;
  8.     protected final long mask;
  9.     protected final double factor;
  10.     protected final long defaultValue;
  11.     private final String name;
  12.     private final long maxValue;
  13.     private final boolean allowZero;
  14.     private final int bits;

  15.     /**
  16.      * Define a bit-encoded value
  17.      * <p>
  18.      *
  19.      * @param name         Description for debugging
  20.      * @param shift        bit index of this value
  21.      * @param bits         number of bits reserved
  22.      * @param factor       scaling factor for stored values
  23.      * @param defaultValue default value
  24.      * @param maxValue     default maximum value
  25.      */
  26.     public EncodedValueOld(String name, int shift, int bits, double factor, long defaultValue, int maxValue) {
  27.         this(name, shift, bits, factor, defaultValue, maxValue, true);
  28.     }

  29.     public EncodedValueOld(String name, int shift, int bits, double factor, long defaultValue, int maxValue, boolean allowZero) {
  30.         this.name = name;
  31.         this.shift = shift;
  32.         this.factor = factor;
  33.         this.defaultValue = defaultValue;
  34.         this.bits = bits;
  35.         long tmpMask = (1L << bits) - 1;
  36.         this.maxValue = Math.min(maxValue, Math.round(tmpMask * factor));
  37.         if (maxValue > this.maxValue)
  38.             throw new IllegalStateException(name + " -> maxValue " + maxValue + " is too large for " + bits + " bits");

  39.         double factorDivision = maxValue / factor;
  40.         if (factorDivision != (int) factorDivision) {
  41.             throw new IllegalStateException("MaxValue needs to be divisible by factor without remainder");
  42.         }

  43.         mask = tmpMask << shift;
  44.         this.allowZero = allowZero;
  45.     }

  46.     protected void checkValue(long value) {
  47.         if (value > maxValue)
  48.             throw new IllegalArgumentException(name + " value too large for encoding: " + value + ", maxValue:" + maxValue);
  49.         if (value < 0)
  50.             throw new IllegalArgumentException("negative " + name + " value not allowed! " + value);
  51.         if (!allowZero && value == 0)
  52.             throw new IllegalArgumentException("zero " + name + " value not allowed! " + value);
  53.     }

  54.     public long setValue(long flags, long value) {
  55.         // scale value
  56.         value = Math.round(value / factor);
  57.         checkValue((long) (value * factor));
  58.         value <<= shift;

  59.         // clear value bits
  60.         flags &= ~mask;

  61.         // set value
  62.         return flags | value;
  63.     }

  64.     public String getName() {
  65.         return name;
  66.     }

  67.     public long getValue(long flags) {
  68.         // find value
  69.         flags &= mask;
  70.         flags >>>= shift;
  71.         return Math.round(flags * factor);
  72.     }

  73.     public int getBits() {
  74.         return bits;
  75.     }

  76.     public double getFactor() {
  77.         return factor;
  78.     }

  79.     public long setDefaultValue(long flags) {
  80.         return setValue(flags, defaultValue);
  81.     }
  82. }