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

com.hfg.util.collection.DirtyProperties Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util.collection;

import java.util.HashSet;
import java.util.Map;
import java.util.Properties;


//------------------------------------------------------------------------------
/**
 * Properties that keeps a dirty flag.
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class DirtyProperties extends Properties
{
   private boolean mIsDirty;

   private HashSet mDirtyProperties = new HashSet<>(25);

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   public DirtyProperties()
   {
      super();
   }

   //--------------------------------------------------------------------------
   /**
    Creates an empty property list with the specified defaults.
    @param   inDefaults   the defaults.
    */
   public DirtyProperties(Properties inDefaults)
   {
      super(inDefaults);
   }


   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   /**
    * Flags all properties as not being dirty.
    */
   public void bless()
   {
      mIsDirty = false;
      mDirtyProperties.clear();
   }

   //--------------------------------------------------------------------------
   /**
    * Returns whether or not the properties set as a whole is dirty (has been changed).
    * @return whether or not the properties set as a whole is dirty.
    */
   public boolean isDirty()
   {
      return mIsDirty;
   }

   //--------------------------------------------------------------------------
   /**
    * Returns whether or not the value for the specified key is dirty (has been changed).
    * @param inKey the specified key to check
    * @return whether or not the value for the specified key is dirty.
    */
   public boolean isDirty(String inKey)
   {
      return mDirtyProperties.contains(inKey);
   }

   //--------------------------------------------------------------------------
   @Override
   public void clear()
   {
      if (size() > 0)
      {
         mIsDirty = true;
         mDirtyProperties.clear();
         super.clear();
      }
   }

   //--------------------------------------------------------------------------
   @Override
   public synchronized Object setProperty(String inKey, String inValue)
   {
      return put(inKey, inValue);
   }

   //--------------------------------------------------------------------------
   @Override
   public Object put(Object inKey, Object inValue)
   {
      boolean valueChanged = valueChanged(inKey, inValue);

      if (! isDirty())
      {
         mIsDirty = valueChanged;
      }

      if (valueChanged)
      {
         mDirtyProperties.add(inKey);
      }

      return super.put(inKey, inValue);
   }

   //--------------------------------------------------------------------------
   @Override
   public void putAll(Map m)
   {
      for (Object key : m.keySet())
      {
         put(key, m.get(key));
      }
   }

   //--------------------------------------------------------------------------
   @Override
   public Object remove(Object inKey)
   {
      if (! isDirty()
            && containsKey(inKey))
      {
         mIsDirty = true;
         mDirtyProperties.remove(inKey);
      }

      return super.remove(inKey);
   }

   //--------------------------------------------------------------------------
   private boolean valueChanged(Object inKey, Object inNewValue)
   {
      Object currentValue = get(inKey);
      return ((null == currentValue
               && inNewValue != null)
              || (currentValue != null
                  && ! currentValue.equals(inNewValue)));
   }

}