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-2015 ForgeRock AS
026 */
027
028package org.opends.server.admin;
029
030
031
032import static org.forgerock.util.Reject.ifNull;
033
034import java.util.EnumSet;
035
036import org.forgerock.i18n.LocalizedIllegalArgumentException;
037import org.forgerock.opendj.ldap.AddressMask;
038
039
040
041/**
042 * IP address mask property definition.
043 */
044public final class IPAddressMaskPropertyDefinition extends
045    PropertyDefinition<AddressMask> {
046
047  /**
048   * An interface for incrementally constructing IP address mask property
049   * definitions.
050   */
051  public static class Builder extends
052      AbstractBuilder<AddressMask, IPAddressMaskPropertyDefinition> {
053
054    /** Private constructor. */
055    private Builder(
056        AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
057      super(d, propertyName);
058    }
059
060
061
062    /** {@inheritDoc} */
063    @Override
064    protected IPAddressMaskPropertyDefinition buildInstance(
065        AbstractManagedObjectDefinition<?, ?> d,
066        String propertyName, EnumSet<PropertyOption> options,
067        AdministratorAction adminAction,
068        DefaultBehaviorProvider<AddressMask> defaultBehavior) {
069      return new IPAddressMaskPropertyDefinition(d, propertyName, options,
070          adminAction, defaultBehavior);
071    }
072
073  }
074
075
076
077  /**
078   * Create a IP address mask property definition builder.
079   *
080   * @param d
081   *          The managed object definition associated with this
082   *          property definition.
083   * @param propertyName
084   *          The property name.
085   * @return Returns the new IP address mask property definition builder.
086   */
087  public static Builder createBuilder(
088      AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
089    return new Builder(d, propertyName);
090  }
091
092
093
094  /** Private constructor. */
095  private IPAddressMaskPropertyDefinition(
096      AbstractManagedObjectDefinition<?, ?> d, String propertyName,
097      EnumSet<PropertyOption> options,
098      AdministratorAction adminAction,
099      DefaultBehaviorProvider<AddressMask> defaultBehavior) {
100    super(d, AddressMask.class, propertyName, options, adminAction,
101        defaultBehavior);
102  }
103
104
105
106  /** {@inheritDoc} */
107  @Override
108  public void validateValue(AddressMask value)
109      throws PropertyException {
110    ifNull(value);
111
112    // No additional validation required.
113  }
114
115
116
117  /** {@inheritDoc} */
118  @Override
119  public AddressMask decodeValue(String value)
120      throws PropertyException {
121    ifNull(value);
122
123    try {
124      return AddressMask.valueOf(value);
125    } catch (LocalizedIllegalArgumentException e) {
126      // TODO: it would be nice to throw the cause.
127      throw PropertyException.illegalPropertyValueException(this, value);
128    }
129  }
130
131
132
133  /** {@inheritDoc} */
134  @Override
135  public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
136    return v.visitIPAddressMask(this, p);
137  }
138
139
140
141  /** {@inheritDoc} */
142  @Override
143  public <R, P> R accept(PropertyValueVisitor<R, P> v, AddressMask value, P p) {
144    return v.visitIPAddressMask(this, value, p);
145  }
146
147
148
149  /** {@inheritDoc} */
150  @Override
151  public int compare(AddressMask o1, AddressMask o2) {
152    return o1.toString().compareTo(o2.toString());
153  }
154}