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

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

package net.alantea.liteprops;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapProperty extends Property> implements Map
{
   private IntegerProperty sizeProperty = new IntegerProperty();
   
   public MapProperty()
   {
      super.setValue(new HashMap<>());
      sizeProperty.set(0);
   }

   public MapProperty(Map value)
   {
      this();
      setValue(value);
   }
   
   public Map get()
   {
      return this;
   }
   
   /**
    * Sets the value.
    *
    * @param newValue the new value
    */
   protected void setValue(Map newValue)
   {
      Map  list = new HashMap<>();
      list.putAll(this);
      super.get().clear();
      super.get().putAll(list);
      fireChanged(list, this);
   }
   
   @Override
   protected void fireChanged(Map oldList, Map newList)
   {
      super.fireChanged(oldList, newList);
      sizeProperty.set(newList.size());
   }

   /**
    * Size property.
    *
    * @return the integer property
    */
   public IntegerProperty sizeProperty()
   {
      return sizeProperty;
   }

   @Override
   public int size()
   {
      return super.get().size();
   }

   @Override
   public boolean isEmpty()
   {
      return super.get().isEmpty();
   }

   @Override
   public void clear()
   {
      if (isEditable())
      {
         super.get().clear();
         fireChanged(this, this);
      }
   }

   @Override
   public boolean containsKey(Object key)
   {
      return super.get().containsKey(key);
   }

   @Override
   public boolean containsValue(Object value)
   {
      return super.get().containsValue(value);
   }

   @Override
   public Q get(Object key)
   {
      return super.get().get(key);
   }

   @Override
   public Q put(P key, Q value)
   {
      Q ret = null;
      if (isEditable())
      {
         ret = super.get().put(key, value);
         fireChanged(this, this);
      }
      return ret;
   }

   @Override
   public Q remove(Object key)
   {
      Q ret = null;
      if (isEditable())
      {
         ret = super.get().remove(key);
         fireChanged(this, this);
      }
      return ret;
   }

   @Override
   public void putAll(Map map)
   {
      if (isEditable())
      {
         super.get().putAll(map);
         fireChanged(this, this);
      }
   }

   @Override
   public Set

keySet() { return super.get().keySet(); } @Override public Collection values() { return super.get().values(); } @Override public Set> entrySet() { return get().entrySet(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy