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

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

package net.alantea.liteprops;

public class IntegerProperty extends Property
{
   public IntegerProperty()
   {
      super(0, true);
   }
   
   protected IntegerProperty(boolean flag)
   {
      super(0, flag);
   }

   public IntegerProperty(int value)
   {
      super(value, true);
   }
   
   protected IntegerProperty(int value, boolean flag)
   {
      super(value, flag);
   }

   /**
    * Add.
    *
    * @param property the property
    * @return the read only property
    */
   public IntegerProperty add(Property property)
   {
      int bret = getIntFrom(this) + getIntFrom(property);
      IntegerProperty ret = new IntegerProperty(false);
      addListener((o, n) -> ret.setValue(getIntFrom(this) +  getIntFrom(property)));
      property.addListener((o, n) -> ret.setValue(getIntFrom(this) +  getIntFrom(property)));
      ret.setValue(bret);
      return ret;
   }
   
   /**
    * minus.
    *
    * @param property the property
    * @return the read only property
    */
   public IntegerProperty minus(Property property)
   {
      int bret = getIntFrom(this) - getIntFrom(property);
      IntegerProperty ret = new IntegerProperty(false);
      addListener((o, n) -> ret.setValue(getIntFrom(this) -  getIntFrom(property)));
      property.addListener((o, n) -> ret.setValue(getIntFrom(this) -  getIntFrom(property)));
      ret.setValue(bret);
      return ret;
   }
   
   /**
    * Nor.
    *
    * @param property the property
    * @return the read only property
    */
   public IntegerProperty divide(Property property)
   {
      int bret = getIntFrom(this) / getIntFrom(property);
      IntegerProperty ret = new IntegerProperty(false);
      addListener((o, n) -> ret.setValue(getIntFrom(this) /  getIntFrom(property)));
      property.addListener((o, n) -> ret.setValue(getIntFrom(this) /  getIntFrom(property)));
      ret.setValue(bret);
      return ret;
   }
   
   /**
    * Nand.
    *
    * @param property the property
    * @return the read only property
    */
   public IntegerProperty multiply(Property property)
   {
      int bret = getIntFrom(this) * getIntFrom(property);
      IntegerProperty ret = new IntegerProperty(false);
      addListener((o, n) -> ret.setValue(getIntFrom(this) *  getIntFrom(property)));
      property.addListener((o, n) -> ret.setValue(getIntFrom(this) *  getIntFrom(property)));
      ret.setValue(bret);
      return ret;
   }
   
   /**
    * Minimum.
    *
    * @param property1 the property 1
    * @param property2 the property 2
    * @return the read only property
    */
   public static IntegerProperty minimum(Property property1, Property property2)
   {
      IntegerProperty ret = new IntegerProperty(false);
      if ((property1.get() != null) && (property2.get() != null))
      {
         ret.setValue(Math.min(getIntFrom(property1), getIntFrom(property2)));
      }
      property1.addListener((o, n) -> ret.setValue(Math.min(getIntFrom(property1), getIntFrom(property2))));
      property2.addListener((o, n) -> ret.setValue(Math.min(getIntFrom(property1), getIntFrom(property2))));
      return ret;
   }
   
   /**
    * Maximum.
    *
    * @param property1 the property 1
    * @param property2 the property 2
    * @return the read only property
    */
   public static IntegerProperty maximum(Property property1, Property property2)
   {
      IntegerProperty ret = new IntegerProperty(false);
      if ((property1.get() != null) && (property2.get() != null))
      {
         ret.setValue(Math.max(getIntFrom(property1), getIntFrom(property2)));
      }
      property1.addListener((o, n) -> ret.setValue(Math.max(getIntFrom(property1), getIntFrom(property2))));
      property2.addListener((o, n) -> ret.setValue(Math.max(getIntFrom(property1), getIntFrom(property2))));
      return ret;
   }
   
   /**
    * Gets the integer from a property.
    *
    * @param property the property
    * @return the integer
    */
   public static int getIntFrom(Property property)
   {
      Object content = property.get();
      if (content instanceof Integer)
      {
         return (Integer)content;
      }
      else if (content instanceof Boolean)
      {
         return ((Boolean)content) ? 1 : 0;
      }
      else if (content instanceof Double)
      {
         return ((Double)content).intValue();
      }
      else if ((content instanceof String) && (((String)content).matches("^\\s*[0-9]+\\s*$")))
      {
         return Integer.parseInt((String)content);
      }
      return 0;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy