001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions copyright 2014 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.config;
019
020import org.forgerock.i18n.LocalizedIllegalArgumentException;
021import org.forgerock.opendj.ldap.AddressMask;
022import org.forgerock.util.Reject;
023
024import java.util.EnumSet;
025
026/**
027 * IP address mask property definition.
028 */
029public final class IPAddressMaskPropertyDefinition extends PropertyDefinition<AddressMask> {
030
031    /**
032     * An interface for incrementally constructing IP address mask property
033     * definitions.
034     */
035    public static final class Builder extends AbstractBuilder<AddressMask, IPAddressMaskPropertyDefinition> {
036
037        /** Private constructor. */
038        private Builder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
039            super(d, propertyName);
040        }
041
042        /** {@inheritDoc} */
043        @Override
044        protected IPAddressMaskPropertyDefinition buildInstance(AbstractManagedObjectDefinition<?, ?> d,
045            String propertyName, EnumSet<PropertyOption> options, AdministratorAction adminAction,
046            DefaultBehaviorProvider<AddressMask> defaultBehavior) {
047            return new IPAddressMaskPropertyDefinition(d, propertyName, options, adminAction, defaultBehavior);
048        }
049
050    }
051
052    /**
053     * Create a IP address mask property definition builder.
054     *
055     * @param d
056     *            The managed object definition associated with this property
057     *            definition.
058     * @param propertyName
059     *            The property name.
060     * @return Returns the new IP address mask property definition builder.
061     */
062    public static Builder createBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
063        return new Builder(d, propertyName);
064    }
065
066    /** Private constructor. */
067    private IPAddressMaskPropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
068        EnumSet<PropertyOption> options, AdministratorAction adminAction,
069        DefaultBehaviorProvider<AddressMask> defaultBehavior) {
070        super(d, AddressMask.class, propertyName, options, adminAction, defaultBehavior);
071    }
072
073    /** {@inheritDoc} */
074    @Override
075    public void validateValue(AddressMask value) {
076        Reject.ifNull(value);
077
078        // No additional validation required.
079    }
080
081    /** {@inheritDoc} */
082    @Override
083    public AddressMask decodeValue(String value) {
084        Reject.ifNull(value);
085
086        try {
087            return AddressMask.valueOf(value);
088        } catch (LocalizedIllegalArgumentException e) {
089            // TODO: it would be nice to throw the cause.
090            throw PropertyException.illegalPropertyValueException(this, value);
091        }
092    }
093
094    /** {@inheritDoc} */
095    @Override
096    public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
097        return v.visitIPAddressMask(this, p);
098    }
099
100    /** {@inheritDoc} */
101    @Override
102    public <R, P> R accept(PropertyValueVisitor<R, P> v, AddressMask value, P p) {
103        return v.visitIPAddressMask(this, value, p);
104    }
105
106    /** {@inheritDoc} */
107    @Override
108    public int compare(AddressMask o1, AddressMask o2) {
109        return o1.toString().compareTo(o2.toString());
110    }
111}