public int add()

in ldap/model/src/main/java/org/apache/directory/api/ldap/model/entry/DefaultAttribute.java [791:1050]


    public int add( Value... vals )
    {
        int nbAdded = 0;
        Value nullBinaryValue = null;
        Value nullStringValue = null;
        boolean nullValueAdded = false;
        Value[] valArray = vals;

        if ( vals == null )
        {
            valArray = new Value[0];
        }

        if ( attributeType != null )
        {
            for ( Value val : valArray )
            {
                if ( attributeType.getSyntax().isHumanReadable() )
                {
                    if ( ( val == null ) || val.isNull() )
                    {
                        try
                        {
                            Value nullSV = new Value( attributeType, ( String ) null );

                            if ( values.add( nullSV ) )
                            {
                                nbAdded++;
                            }
                        }
                        catch ( LdapInvalidAttributeValueException iae )
                        {
                            continue;
                        }
                    }
                    else if ( val.isHumanReadable() )
                    {
                        try
                        {
                            if ( val.getAttributeType() == null )
                            {
                                val = new Value( attributeType, val  );
                            }

                            if ( values.contains( val ) )
                            {
                                // Replace the value
                                values.remove( val );
                                values.add( val );
                            }
                            else if ( values.add( val ) )
                            {
                                nbAdded++;
                            }
                        }
                        catch ( LdapInvalidAttributeValueException iae )
                        {
                            continue;
                        }
                    }
                    else
                    {
                        String message = I18n.err( I18n.ERR_13213_VALUE_MUST_BE_A_STRING );
                        LOG.error( message );
                    }
                }
                else
                {
                    if ( val == null )
                    {
                        if ( attributeType.getSyntax().getSyntaxChecker().isValidSyntax( val ) )
                        {
                            try
                            {
                                Value nullSV = new Value( attributeType, ( byte[] ) null );

                                if ( values.add( nullSV ) )
                                {
                                    nbAdded++;
                                }
                            }
                            catch ( LdapInvalidAttributeValueException iae )
                            {
                                continue;
                            }
                        }
                        else
                        {
                            String message = I18n.err( I18n.ERR_13211_BYTE_VALUE_EXPECTED );
                            LOG.error( message );
                        }
                    }
                    else
                    {
                        if ( !val.isHumanReadable() )
                        {
                            try
                            {
                                if ( val.getAttributeType() == null )
                                {
                                    val = new Value( attributeType, val.getBytes() );
                                }

                                if ( values.add( val ) )
                                {
                                    nbAdded++;
                                }
                            }
                            catch ( LdapInvalidAttributeValueException iae )
                            {
                                continue;
                            }
                        }
                        else
                        {
                            String message = I18n.err( I18n.ERR_13211_BYTE_VALUE_EXPECTED );
                            LOG.error( message );
                        }
                    }
                }
            }
        }
        else
        {
            for ( Value val : valArray )
            {
                if ( val == null )
                {
                    // We have a null value. If the HR flag is not set, we will consider
                    // that the attribute is not HR. We may change this later
                    if ( isHR == null )
                    {
                        // This is the first value. Add both types, as we
                        // don't know yet the attribute type's, but we may
                        // know later if we add some new value.
                        // We have to do that because we are using a Set,
                        // and we can't remove the first element of the Set.
                        nullBinaryValue = new Value( ( byte[] ) null );
                        nullStringValue = new Value( ( String ) null );

                        values.add( nullBinaryValue );
                        values.add( nullStringValue );
                        nullValueAdded = true;
                        nbAdded++;
                    }
                    else if ( !isHR )
                    {
                        // The attribute type is binary.
                        nullBinaryValue = new Value( ( byte[] ) null );

                        // Don't add a value if it already exists.
                        if ( !values.contains( nullBinaryValue ) )
                        {
                            values.add( nullBinaryValue );
                            nbAdded++;
                        }

                    }
                    else
                    {
                        // The attribute is HR
                        nullStringValue = new Value( ( String ) null );

                        // Don't add a value if it already exists.
                        if ( !values.contains( nullStringValue ) )
                        {
                            values.add( nullStringValue );
                        }
                    }
                }
                else
                {
                    // Let's check the value type.
                    if ( val.isHumanReadable() )
                    {
                        // We have a String value
                        if ( isHR == null )
                        {
                            // The attribute type will be set to HR
                            isHR = true;
                            values.add( val );
                            nbAdded++;
                        }
                        else if ( !isHR )
                        {
                            // The attributeType is binary, convert the
                            // value to a Value
                            Value bv = new Value( val.getBytes() );

                            if ( !contains( bv ) )
                            {
                                values.add( bv );
                                nbAdded++;
                            }
                        }
                        else
                        {
                            // The attributeType is HR, simply add the value
                            if ( !contains( val ) )
                            {
                                values.add( val );
                                nbAdded++;
                            }
                        }
                    }
                    else
                    {
                        // We have a Binary value
                        if ( isHR == null )
                        {
                            // The attribute type will be set to binary
                            isHR = false;
                            values.add( val );
                            nbAdded++;
                        }
                        else if ( !isHR )
                        {
                            // The attributeType is not HR, simply add the value if it does not already exist
                            if ( !contains( val ) )
                            {
                                values.add( val );
                                nbAdded++;
                            }
                        }
                        else
                        {
                            // The attribute Type is HR, convert the
                            // value to a Value
                            Value sv = new Value( Strings.utf8ToString( val.getBytes() ) );

                            if ( !contains( sv ) )
                            {
                                values.add( sv );
                                nbAdded++;
                            }
                        }
                    }
                }
            }
        }

        // Last, not least, if a nullValue has been added, and if other
        // values are all String, we have to keep the correct nullValue,
        // and to remove the other
        if ( nullValueAdded )
        {
            if ( isHR )
            {
                // Remove the Binary value
                values.remove( nullBinaryValue );
            }
            else
            {
                // Remove the String value
                values.remove( nullStringValue );
            }
        }

        return nbAdded;
    }