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

com.phloc.html.js.builder.JSCommentMultiLine 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.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

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;

/**
 * JSDoc comment.
 *
 * @author Philip Helger
 */
public class JSCommentMultiLine extends JSCommentPart implements IJSGeneratable
{
  private static final String INDENT = " *     ";

  /** list of @-param tags */
  private final Map  m_aParams = new LinkedHashMap  ();

  /** list of xdoclets */
  private final Map > m_aXDoclets = new LinkedHashMap > ();

  /** The @-return tag part. */
  private JSCommentPart m_aReturn;

  /** The @-deprecated tag */
  private JSCommentPart m_aDeprecated;

  public JSCommentMultiLine ()
  {}

  @Override
  @Nonnull
  public JSCommentMultiLine append (@Nullable final Object aObj)
  {
    add (aObj);
    return this;
  }

  /**
   * Append a text to a @-param tag to the JSDoc
   */
  @Nonnull
  public JSCommentPart addParam (@Nonnull final String sParam)
  {
    ValueEnforcer.notNull (sParam, "Param");

    JSCommentPart aPart = m_aParams.get (sParam);
    if (aPart == null)
    {
      aPart = new JSCommentPart ();
      m_aParams.put (sParam, aPart);
    }
    return aPart;
  }

  /**
   * Append a text to an @-param tag.
   */
  @Nonnull
  public JSCommentPart addParam (@Nonnull final JSVar aParam)
  {
    return addParam (aParam.name ());
  }

  /**
   * Appends a text to @return tag.
   */
  @Nonnull
  public JSCommentPart addReturn ()
  {
    if (m_aReturn == null)
      m_aReturn = new JSCommentPart ();
    return m_aReturn;
  }

  /**
   * add an @-deprecated tag to the JSDoc, with the associated message.
   */
  @Nonnull
  public JSCommentPart addDeprecated ()
  {
    if (m_aDeprecated == null)
      m_aDeprecated = new JSCommentPart ();
    return m_aDeprecated;
  }

  /**
   * add an xdoclet.
   */
  @Nonnull
  public Map  addXdoclet (@Nonnull final String sName)
  {
    ValueEnforcer.notNull (sName, "Name");

    Map  aMap = m_aXDoclets.get (sName);
    if (aMap == null)
    {
      aMap = new HashMap  ();
      m_aXDoclets.put (sName, aMap);
    }
    return aMap;
  }

  /**
   * add an xdoclet.
   */
  @Nonnull
  public Map  addXdoclet (@Nonnull final String sName, @Nonnull final Map  aAttributes)
  {
    final Map  p = addXdoclet (sName);
    p.putAll (aAttributes);
    return p;
  }

  /**
   * add an xdoclet.
   */
  public Map  addXdoclet (@Nonnull final String sName,
                                          @Nonnull final String sAttributeName,
                                          @Nonnull final String sAttributeValue)
  {
    final Map  p = addXdoclet (sName);
    p.put (sAttributeName, sAttributeValue);
    return p;
  }

  public void generate (@Nonnull final JSFormatter aFormatter)
  {
    if (!aFormatter.generateComments ())
      return;

    aFormatter.plain ("/**").nlFix ();

    // Main content start
    format (aFormatter, " * ");

    if (!m_aParams.isEmpty () || m_aReturn != null || m_aDeprecated != null || !m_aXDoclets.isEmpty ())
    {
      aFormatter.plain (" * ").nlFix ();
      for (final Map.Entry  aEntry : m_aParams.entrySet ())
      {
        aFormatter.plain (" * @param ").plain (aEntry.getKey ()).nlFix ();
        aEntry.getValue ().format (aFormatter, INDENT);
      }
      if (m_aReturn != null)
      {
        aFormatter.plain (" * @return").nlFix ();
        m_aReturn.format (aFormatter, INDENT);
      }
      if (m_aDeprecated != null)
      {
        aFormatter.plain (" * @deprecated").nlFix ();
        m_aDeprecated.format (aFormatter, INDENT);
      }
      for (final Map.Entry > aEntry : m_aXDoclets.entrySet ())
      {
        aFormatter.plain (" * @").plain (aEntry.getKey ());
        if (aEntry.getValue () != null)
        {
          for (final Map.Entry  aEntry2 : aEntry.getValue ().entrySet ())
            aFormatter.plain (" ").plain (aEntry2.getKey ()).plain ("= \"").plain (aEntry2.getValue ()).plain ("\"");
        }
        aFormatter.nlFix ();
      }
    }
    aFormatter.plain (" */").nlFix ();
  }

  @Nonnull
  public String getJSCode ()
  {
    return JSPrinter.getAsString (this);
  }

  @Override
  public boolean equals (final Object o)
  {
    if (o == this)
      return true;
    if (o == null || !getClass ().equals (o.getClass ()))
      return false;
    final JSCommentMultiLine rhs = (JSCommentMultiLine) o;
    return m_aParams.equals (rhs.m_aParams) &&
           m_aXDoclets.equals (rhs.m_aXDoclets) &&
           EqualsUtils.equals (m_aReturn, rhs.m_aReturn) &&
           EqualsUtils.equals (m_aDeprecated, rhs.m_aDeprecated);
  }

  @Override
  public int hashCode ()
  {
    return new HashCodeGenerator (this).append (m_aParams)
                                       .append (m_aXDoclets)
                                       .append (m_aReturn)
                                       .append (m_aDeprecated)
                                       .getHashCode ();
  }

  @Override
  public String toString ()
  {
    return new ToStringGenerator (this).append ("params", m_aParams)
                                       .append ("xdoclets", m_aXDoclets)
                                       .append ("return", m_aReturn)
                                       .append ("deprecated", m_aDeprecated)
                                       .toString ();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy