# AttributeDefinition.java

    /*
    ** Module   :AttributeDefinition.java
    ** Abstract :An holder for DirectoryService class attribute description.
    **
    ** Copyright (c) 2005-2025, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- -T- --JPRM-- ----------------Description-----------------
    ** 001 SIY 20050217 ADD  @20012  Created initial implementation
    ** 002 SIY 20050310 CHG  @20352  Reordered methods and members.
    ** 003 SIY 20050323 CHG  @20465  Fixed formatting and comments.
    ** 004 SIY 20050429 CHG  @21004  Fixed formatting.
    ** 005 SIY 20050508 CHG  @21185  Added isCompatible() method.
    ** 006 SP  20250416              Code formatting fixes.
    */

    /*
    ** 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;

    /**
     * AttributeDefinition provides information about particular attribute of the
     * Directory node class. Since this information is static, there is no way to
     * change it in particular instance of this class. For the same reason there
     * is no public constructor for the class.
     *
     * @author  SIY
     * @version 1.0
     */
    public final class AttributeDefinition
    {
       /** Attribute is for read-only access */
       private boolean immutable;

       /** 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 size */
       private int size;

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

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

       /**
        * 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
        *          Can attribute contain multiple values?
        * @param   immutable
        *          Can attribute be changed?
        */
       AttributeDefinition(String name, int type, boolean mandatory,
                           boolean multiple, boolean immutable)
       {
          this.name = new String(name);
          this.type = type;
          this.mandatory = mandatory;
          this.multiple = multiple;
          this.immutable = immutable;

          switch (type)
          {
             case AttributeType.ATTR_INTEGER:
                this.size = 4;
                break;

             case AttributeType.ATTR_BOOLEAN:
                this.size = 1;
                break;

             case AttributeType.ATTR_DOUBLE:
             case AttributeType.ATTR_DATE:
             case AttributeType.ATTR_TIME:
                this.size = 8;
                break;

             case AttributeType.ATTR_STRING:
             case AttributeType.ATTR_BYTEARRAY:
             case AttributeType.ATTR_BITFIELD:
             case AttributeType.ATTR_BITSELECTOR:
                this.size = 0;
                break;
          }
       }

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

       /**
        * Returns exact size for fixed size attributes and 0 for unlimited size
        * strings. The returned value is either the number of bytes for binary
        * attributes, or the number of characters.
        *
        * @return  attribute size.
        */
       public int getSize()
       {
          return size;
       }

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

       /**
        * Returns <code>true</code> if attribute is read only.
        *
        * @return  <code>true</code> if attribute is read only.
        */
       public boolean isImmutable()
       {
          return immutable;
       }

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

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

       /**
        * Compare definition with given instance of <code>NodeAttribute</code>
        * and decide if they are compatible by the means of name, type, etc.
        *
        * @param   definition
        *          Instance to compare with.
        *
        * @return  <code>true</code> if attribute definitions are compatible.
        */
       public boolean isCompatible(NodeAttribute definition)
       {
          if (!name.equals(definition.getName()))
          {
             return false;
          }
          if (definition.getType() != type)
          {
             return false;
          }
          if (definition.isMandatory() != mandatory)
          {
             return false;
          }
          if (definition.isMultiple() != multiple)
          {
             return false;
          }
          return true;
       }

       /**
        * Generate NodeAttribute from this definition.
        *
        * @return  an instance of the NodeAttribute
        */
       NodeAttribute genNodeAttribute()
       {
          return new NodeAttribute(name, type, mandatory, multiple, 0);
       }
    }