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