# NodeAttribute.java

    /*
    ** Module   :NodeAttribute.java
    ** Abstract :An holder for description of the current state of the attribute.
    **
    ** Copyright (c) 2005-2020, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- -T- --JPRM-- ----------------Description-----------------
    ** 001 SIY 20050216 ADD  @20019  Created initial implementation
    ** 002 SIY 20050310 CHG  @19710  Reordered members, fixed typos, added
    **                               toString() method.
    ** 003 NVS 20050426 CHG  @20891  Class implements Serializable interface.
    ** 004 SIY 20050429 CHG  @21021  Fixed comments and formatting.
    ** 005 IAS 20200914 Re-work (de)serialization
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program 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 Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.directory;

    import static com.goldencode.util.NativeTypeSerializer.*;
    import java.io.*;

    /**
     * NodeAttribute provides information about particular DirectoryService node
     * attribute in its current state. It is used to represent results of the
     * enumeration of the node attributes.
     *
     * @author  SIY
     * @version 1.0
     */
    public final class NodeAttribute
    implements Externalizable
    {
       /** Number of the values assigned to the attribute. */
       private int count;

       /** Is attribute mandatory for the node or not */
       private boolean mandatory;

       /** Can attribute contain multiple values or not */
       private boolean multiple;

       /** Attribute name. */
       private String name;

       /** Attribute type. */
       private int type;

       /** Default constructor */
       public NodeAttribute()
       {
          super();
       }

       /**
        * Constructs an instance using other NodeAttribute as a template
        *
        * @param   other
        *          Source instance to copy from.
        */
       NodeAttribute(NodeAttribute other)
       {
          this.name = new String(other.name);
          this.type = other.type;
          this.mandatory = other.mandatory;
          this.multiple = other.multiple;
          this.count = other.count;
       }

       /**
        * Constructs an instance of node attribute. Note that constructor is
        * package private.
        *
        * @param   name
        *          Attribute name.
        * @param   type
        *          attribute type (one of ATTR_* values).
        * @param   mandatory
        *          Is attribute mandatory?
        * @param   multiple
        *          Is attribute contains multiple values?
        * @param   count
        *          Number of values assigned to the attribute.
        */
       NodeAttribute(String name, int type, boolean mandatory, boolean multiple,
                     int count)
       {
          this.name = new String(name);
          this.type = type;
          this.mandatory = mandatory;
          this.multiple = multiple;
          this.count = count;
       }

       /**
        * Returns number of values assigned to the attribute.
        *
        * @return  Number of values assigned to attribute.
        */
       public int getCount()
       {
          return count;
       }

       /**
        * Returns the value of name.
        *
        * @return  Attribute name.
        */
       public String getName()
       {
          return name;
       }

       /**
        * Returns the value of type.
        *
        * @return  Attribute type.
        */
       public int getType()
       {
          return type;
       }

       /**
        * Returns <code>true</code> if attribute is mandatory.
        *
        * @return  <code>true</code> if attribute is mandatory.
        */
       public boolean isMandatory()
       {
          return mandatory;
       }

       /**
        * Returns <code>true</code> if attribute can contain multiple values.
        *
        * @return  <code>true</code> if attribute can contain multiple values.
        */
       public boolean isMultiple()
       {
          return multiple;
       }

       /**
        * Present <code>NodeAttribute</code> information in user readable form.
        *
        * @see     java.lang.Object#toString()
        * @return  <code>NodeAttribute</code> data as human-readable
        *          <code>String</code>.
        */
       public String toString()
       {
          String res = "nodeattribute <" + name + "> "
             + (multiple ? "multiple, " : "single, ")
             + (mandatory ? "mandatory, " : "optional, ") + "type "
             + AttributeType.toString(type) + ", values " + count;

          return res;
       }

       /**
        * Update value counter.
        *
        * @param   count
        *          New number of values assigned to the attribute.
        */
       void setCount(int count)
       {
          this.count = count;
       }

       /**
        * Replacement for the default object writing method.
        *
        * @param    out
        *           The output destination to which fields will be saved.
        *
        * @throws   IOException
        *           In case of I/O errors.
        */
       @Override
       public void writeExternal(ObjectOutput out) throws IOException
       {
          out.writeInt(count);
          out.writeBoolean(mandatory);
          out.writeBoolean(multiple);
          writeString(out, name);
          out.writeInt(type);

       }

       /**
        * Replacement for the default object reading method.
        *
        * @param    in
        *           Input source from which fields will be restored.
        *
        * @throws   IOException
        *           In case of I/O errors.
        * @throws   ClassNotFoundException
        *           If payload can't be instantiated.
        */
       @Override
       public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
       {
          count = in.readInt();
          mandatory = in.readBoolean();
          multiple = in.readBoolean();
          name = readString(in);
          type = in.readInt();
       }
    }