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

com.helger.settings.Settings Maven / Gradle / Ivy

/**
 * Copyright (C) 2014-2016 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.settings;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.OverrideOnDemand;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.ext.CommonsHashMap;
import com.helger.commons.collection.ext.ICommonsMap;
import com.helger.commons.collection.ext.ICommonsSet;
import com.helger.commons.equals.EqualsHelper;
import com.helger.commons.hashcode.HashCodeGenerator;
import com.helger.commons.state.EChange;
import com.helger.commons.string.ToStringGenerator;

/**
 * The default implementation of the {@link IMutableSettings} object.
 *
 * @author philip
 */
public class Settings implements IMutableSettings
{
  private final String m_sName;
  private final ICommonsMap  m_aMap = new CommonsHashMap <> ();

  public Settings (@Nonnull @Nonempty final String sName)
  {
    m_sName = ValueEnforcer.notEmpty (sName, "Name");
  }

  @Nonnull
  @Nonempty
  public final String getName ()
  {
    return m_sName;
  }

  @Nonnegative
  public int getSize ()
  {
    return m_aMap.size ();
  }

  public boolean isEmpty ()
  {
    return m_aMap.isEmpty ();
  }

  @Nonnull
  @ReturnsMutableCopy
  public final ICommonsSet  getAllFieldNames ()
  {
    return m_aMap.copyOfKeySet ();
  }

  @Nonnull
  @ReturnsMutableCopy
  public ICommonsMap  getAllEntries ()
  {
    return m_aMap.getClone ();
  }

  public boolean containsField (@Nullable final String sFieldName)
  {
    return m_aMap.containsKey (sFieldName);
  }

  @Nullable
  public Object getValue (@Nullable final String sFieldName)
  {
    return m_aMap.get (sFieldName);
  }

  @Nullable
  public IMutableSettings getSettingsValue (@Nullable final String sFieldName)
  {
    return getConvertedValue (sFieldName, IMutableSettings.class);
  }

  public void restoreValue (@Nonnull @Nonempty final String sFieldName, @Nonnull final Object aNewValue)
  {
    ValueEnforcer.notEmpty (sFieldName, "FieldName");
    ValueEnforcer.notNull (aNewValue, "NewValue");

    m_aMap.put (sFieldName, aNewValue);
  }

  @Nonnull
  public EChange setValues (@Nonnull final ISettings aOtherSettings)
  {
    ValueEnforcer.notNull (aOtherSettings, "OtherSettings");

    EChange eChange = EChange.UNCHANGED;
    for (final String sFieldName : aOtherSettings.getAllFieldNames ())
      eChange = eChange.or (setValue (sFieldName, aOtherSettings.getValue (sFieldName)));
    return eChange;
  }

  @Nonnull
  public EChange removeValue (@Nullable final String sFieldName)
  {
    return EChange.valueOf (m_aMap.remove (sFieldName) != null);
  }

  @Nonnull
  public EChange clear ()
  {
    return m_aMap.removeAll ();
  }

  /**
   * Protected method that is invoked after a setting changed.
   *
   * @param sFieldName
   *        The changed field name. Neither null nor empty.
   * @param aOldValue
   *        The old value. May be null.
   * @param aNewValue
   *        The new value. May be null in which case the value was
   *        removed.
   */
  @OverrideOnDemand
  protected void onAfterSettingsChanged (@Nonnull @Nonempty final String sFieldName,
                                         @Nullable final Object aOldValue,
                                         @Nullable final Object aNewValue)
  {}

  @Nonnull
  public EChange setValue (@Nonnull @Nonempty final String sFieldName, @Nullable final Object aNewValue)
  {
    ValueEnforcer.notEmpty (sFieldName, "FieldName");

    // Get the old value
    final Object aOldValue = getValue (sFieldName);
    if (EqualsHelper.equals (aOldValue, aNewValue))
      return EChange.UNCHANGED;

    // Value changed -> trigger update
    if (aNewValue == null)
      m_aMap.remove (sFieldName);
    else
      m_aMap.put (sFieldName, aNewValue);

    onAfterSettingsChanged (sFieldName, aOldValue, aNewValue);
    return EChange.CHANGED;
  }

  @Override
  public boolean equals (final Object o)
  {
    if (o == this)
      return true;
    if (o == null || !getClass ().equals (o.getClass ()))
      return false;
    final Settings rhs = (Settings) o;
    return m_sName.equals (rhs.m_sName) && EqualsHelper.equals (m_aMap, rhs.m_aMap);
  }

  @Override
  public int hashCode ()
  {
    return new HashCodeGenerator (this).append (m_sName).append (m_aMap).getHashCode ();
  }

  @Override
  public String toString ()
  {
    return new ToStringGenerator (this).append ("name", m_sName).append ("map", m_aMap).toString ();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy