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-2016 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/** IP address mask property definition. */
027public final class IPAddressMaskPropertyDefinition extends PropertyDefinition<AddressMask> {
028
029    /** An interface for incrementally constructing IP address mask property definitions. */
030    public static final class Builder extends AbstractBuilder<AddressMask, IPAddressMaskPropertyDefinition> {
031
032        /** Private constructor. */
033        private Builder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
034            super(d, propertyName);
035        }
036
037        @Override
038        protected IPAddressMaskPropertyDefinition buildInstance(AbstractManagedObjectDefinition<?, ?> d,
039            String propertyName, EnumSet<PropertyOption> options, AdministratorAction adminAction,
040            DefaultBehaviorProvider<AddressMask> defaultBehavior) {
041            return new IPAddressMaskPropertyDefinition(d, propertyName, options, adminAction, defaultBehavior);
042        }
043
044    }
045
046    /**
047     * Create a IP address mask property definition builder.
048     *
049     * @param d
050     *            The managed object definition associated with this property
051     *            definition.
052     * @param propertyName
053     *            The property name.
054     * @return Returns the new IP address mask property definition builder.
055     */
056    public static Builder createBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
057        return new Builder(d, propertyName);
058    }
059
060    /** Private constructor. */
061    private IPAddressMaskPropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
062        EnumSet<PropertyOption> options, AdministratorAction adminAction,
063        DefaultBehaviorProvider<AddressMask> defaultBehavior) {
064        super(d, AddressMask.class, propertyName, options, adminAction, defaultBehavior);
065    }
066
067    @Override
068    public void validateValue(AddressMask value) {
069        Reject.ifNull(value);
070
071        // No additional validation required.
072    }
073
074    @Override
075    public AddressMask decodeValue(String value) {
076        Reject.ifNull(value);
077
078        try {
079            return AddressMask.valueOf(value);
080        } catch (LocalizedIllegalArgumentException e) {
081            // TODO: it would be nice to throw the cause.
082            throw PropertyException.illegalPropertyValueException(this, value);
083        }
084    }
085
086    @Override
087    public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
088        return v.visitIPAddressMask(this, p);
089    }
090
091    @Override
092    public <R, P> R accept(PropertyValueVisitor<R, P> v, AddressMask value, P p) {
093        return v.visitIPAddressMask(this, value, p);
094    }
095
096    @Override
097    public int compare(AddressMask o1, AddressMask o2) {
098        return o1.toString().compareTo(o2.toString());
099    }
100}