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

net.alantea.liteprops.DoubleProperty Maven / Gradle / Ivy

package net.alantea.liteprops;

public class DoubleProperty extends Property
{
   public DoubleProperty()
   {
      super(true);
   }
   
   protected DoubleProperty(boolean flag)
   {
      super(flag);
   }
   
   public DoubleProperty(double value)
   {
      super(value, true);
   }
   
   protected DoubleProperty(double value, boolean flag)
   {
      super(value, flag);
   }

   /**
    * Add.
    *
    * @param property the property
    * @return the read only property
    */
   public DoubleProperty add(Property property)
   {
      double bret = getDoubleFrom(this) + getDoubleFrom(property);
      DoubleProperty ret = new DoubleProperty(false);
      addListener((o, n) -> ret.setValue(getDoubleFrom(this) +  getDoubleFrom(property)));
      property.addListener((o, n) -> ret.setValue(getDoubleFrom(this) +  getDoubleFrom(property)));
      ret.setValue(bret);
      return ret;
   }
   
   /**
    * minus.
    *
    * @param property the property
    * @return the read only property
    */
   public DoubleProperty minus(Property property)
   {
      double bret = getDoubleFrom(this) - getDoubleFrom(property);
      DoubleProperty ret = new DoubleProperty(false);
      addListener((o, n) -> ret.setValue(getDoubleFrom(this) -  getDoubleFrom(property)));
      property.addListener((o, n) -> ret.setValue(getDoubleFrom(this) -  getDoubleFrom(property)));
      ret.setValue(bret);
      return ret;
   }
   
   /**
    * Nor.
    *
    * @param property the property
    * @return the read only property
    */
   public DoubleProperty divide(Property property)
   {
      double bret = getDoubleFrom(this) / getDoubleFrom(property);
      DoubleProperty ret = new DoubleProperty(false);
      addListener((o, n) -> ret.setValue(getDoubleFrom(this) /  getDoubleFrom(property)));
      property.addListener((o, n) -> ret.setValue(getDoubleFrom(this) /  getDoubleFrom(property)));
      ret.setValue(bret);
      return ret;
   }
   
   /**
    * Nand.
    *
    * @param property the property
    * @return the read only property
    */
   public DoubleProperty multiply(Property property)
   {
      double bret = getDoubleFrom(this) * getDoubleFrom(property);
      DoubleProperty ret = new DoubleProperty(false);
      addListener((o, n) -> ret.setValue(getDoubleFrom(this) *  getDoubleFrom(property)));
      property.addListener((o, n) -> ret.setValue(getDoubleFrom(this) *  getDoubleFrom(property)));
      ret.setValue(bret);
      return ret;
   }
   
   /**
    * Minimum.
    *
    * @param property1 the property 1
    * @param property2 the property 2
    * @return the read only property
    */
   public static DoubleProperty minimum(Property property1, Property property2)
   {
      DoubleProperty ret = new DoubleProperty();
      if ((property1.get() != null) && (property2.get() != null))
      {
         ret.setValue(Math.min(getDoubleFrom(property1), getDoubleFrom(property2)));
      }
      property1.addListener((o, n) -> ret.setValue(Math.min(getDoubleFrom(property1), getDoubleFrom(property2))));
      property2.addListener((o, n) -> ret.setValue(Math.min(getDoubleFrom(property1), getDoubleFrom(property2))));
      return ret;
   }
   
   /**
    * Maximum.
    *
    * @param property1 the property 1
    * @param property2 the property 2
    * @return the read only property
    */
   public static DoubleProperty maximum(Property property1, Property property2)
   {
      DoubleProperty ret = new DoubleProperty();
      if ((property1.get() != null) && (property2.get() != null))
      {
         ret.setValue(Math.max(getDoubleFrom(property1), getDoubleFrom(property2)));
      }
      property1.addListener((o, n) -> ret.setValue(Math.max(getDoubleFrom(property1), getDoubleFrom(property2))));
      property2.addListener((o, n) -> ret.setValue(Math.max(getDoubleFrom(property1), getDoubleFrom(property2))));
      return ret;
   }
   
   /**
    * Gets the double from a property.
    *
    * @param property the property
    * @return the double
    */
   public static double getDoubleFrom(Property property)
   {
      Object content = property.get();
      if (content instanceof Double)
      {
         return (Double)content;
      }
      else if (content instanceof Boolean)
      {
         return ((Boolean)content) ? 1.0 : 0.0;
      }
      else if (content instanceof Integer)
      {
         return ((Integer)content).intValue();
      }
      else if (content instanceof String)
      {
         try
         {
            return Integer.parseInt((String)content);
         }
         catch (NumberFormatException e)
         {
            return 0.0;
         }
      }
      return 0.0;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy