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

com.phloc.html.js.builder.JSAssocArray Maven / Gradle / Ivy

There is a newer version: 4.4.9
Show newest version
/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[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.phloc.html.js.builder;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.Map;

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

import com.phloc.commons.ValueEnforcer;
import com.phloc.commons.equals.EqualsUtils;
import com.phloc.commons.hash.HashCodeGenerator;
import com.phloc.commons.string.ToStringGenerator;
import com.phloc.html.hc.IHCNode;
import com.phloc.html.hc.conversion.HCSettings;
import com.phloc.html.js.marshal.JSMarshaller;
import com.phloc.json.IJSON;

/**
 * array creation and initialization.
 *
 * @author Philip Helger
 */
public class JSAssocArray extends AbstractJSExpression
{
  private Map  m_aExprs;

  public JSAssocArray ()
  {}

  public boolean isEmpty ()
  {
    return this.m_aExprs == null || this.m_aExprs.isEmpty ();
  }

  @Nonnegative
  public int size ()
  {
    return this.m_aExprs == null ? 0 : this.m_aExprs.size ();
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, final boolean bValue)
  {
    return add (sKey, JSExpr.lit (bValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, final char cValue)
  {
    return add (sKey, JSExpr.lit (cValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, final double dValue)
  {
    return add (sKey, JSExpr.lit (dValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, final float fValue)
  {
    return add (sKey, JSExpr.lit (fValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, final int nValue)
  {
    return add (sKey, JSExpr.lit (nValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, final long nValue)
  {
    return add (sKey, JSExpr.lit (nValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, @Nullable final BigDecimal aValue)
  {
    return add (sKey, aValue == null ? JSExpr.NULL : JSExpr.lit (aValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, @Nullable final BigInteger aValue)
  {
    return add (sKey, aValue == null ? JSExpr.NULL : JSExpr.lit (aValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, @Nullable final String sValue)
  {
    return add (sKey, sValue == null ? JSExpr.NULL : JSExpr.lit (sValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, @Nullable final IJSON aValue)
  {
    return add (sKey, aValue == null ? JSExpr.NULL : JSExpr.json (aValue));
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, @Nullable final IHCNode aValue)
  {
    return add (sKey, aValue == null ? null : HCSettings.getAsHTMLStringWithoutNamespaces (aValue));
  }

  @Nonnull
  public JSAssocArray addAllStrings (@Nullable final Map  aValues)
  {
    if (aValues != null)
      for (final Map.Entry  aEntry : aValues.entrySet ())
        add (aEntry.getKey (), JSExpr.lit (aEntry.getValue ()));
    return this;
  }

  @Nonnull
  public JSAssocArray addAll (@Nullable final Map  aValues)
  {
    if (aValues != null)
      for (final Map.Entry  aEntry : aValues.entrySet ())
        add (aEntry.getKey (), aEntry.getValue ());
    return this;
  }

  @Nonnull
  public JSAssocArray add (@Nonnull final String sKey, @Nonnull final IJSExpression aValue)
  {
    // Don't quote value identifiers
    if (JSMarshaller.isJSIdentifier (sKey))
      return add (new JSAtom (sKey), aValue);

    return add (JSExpr.lit (sKey), aValue);
  }

  /**
   * Add an element to the array initializer
   * 
   * @param aKey
   *        key to use
   * @param aValue
   *        value to use
   * @return this
   */
  @Nonnull
  public JSAssocArray add (@Nonnull final IJSExpression aKey, @Nonnull final IJSExpression aValue)
  {
    ValueEnforcer.notNull (aKey, "Key");
    ValueEnforcer.notNull (aValue, "Value");

    if (this.m_aExprs == null)
      this.m_aExprs = new LinkedHashMap  ();
    this.m_aExprs.put (aKey, aValue);
    return this;
  }

  @Nonnull
  public JSAssocArray remove (@Nonnull final String sKey)
  {
    if (this.m_aExprs != null)
      remove (JSExpr.lit (sKey));
    return this;
  }

  @Nonnull
  public JSAssocArray remove (@Nullable final IJSExpression aKey)
  {
    if (this.m_aExprs != null)
      this.m_aExprs.remove (aKey);
    return this;
  }

  @Override
  public void generate (@Nonnull final JSFormatter aFormatter)
  {
    aFormatter.plain ('{').nl ().indent ();
    if (this.m_aExprs != null)
    {
      boolean bFirst = true;
      for (final Map.Entry  aEntry : this.m_aExprs.entrySet ())
      {
        if (bFirst)
          bFirst = false;
        else
          aFormatter.plain (',').nl ();
        aFormatter.generatable (aEntry.getKey ()).plain (':').generatable (aEntry.getValue ());
      }
    }
    aFormatter.nl ().outdent ().plain ('}');
  }

  @Override
  public boolean equals (final Object o)
  {
    if (o == this)
      return true;
    if (!super.equals (o))
      return false;
    final JSAssocArray rhs = (JSAssocArray) o;
    return EqualsUtils.equals (this.m_aExprs, rhs.m_aExprs);
  }

  @Override
  public int hashCode ()
  {
    return HashCodeGenerator.getDerived (super.hashCode ()).append (this.m_aExprs).getHashCode ();
  }

  @Override
  public String toString ()
  {
    return ToStringGenerator.getDerived (super.toString ()).append ("exprs", this.m_aExprs).toString ();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy