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-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2012-2016 ForgeRock AS.
016 */
017package org.opends.server.types;
018
019import java.util.Collection;
020import java.util.Iterator;
021import java.util.List;
022
023import org.forgerock.opendj.ldap.AttributeDescription;
024import org.forgerock.opendj.ldap.ByteString;
025import org.forgerock.opendj.ldap.ConditionResult;
026import org.forgerock.opendj.ldap.schema.AttributeType;
027import org.forgerock.util.Utils;
028import org.opends.server.api.VirtualAttributeProvider;
029
030/**
031 * This class defines a virtual attribute, which is a special kind of
032 * attribute whose values do not actually exist in persistent storage
033 * but rather are computed or otherwise obtained dynamically.
034 */
035@org.opends.server.types.PublicAPI(
036    stability = org.opends.server.types.StabilityLevel.VOLATILE,
037    mayInstantiate = false,
038    mayExtend = false,
039    mayInvoke = true)
040public final class VirtualAttribute
041  extends AbstractAttribute
042{
043  /** The attribute description. */
044  private final AttributeDescription attributeDescription;
045  /** The entry with which this virtual attribute is associated. */
046  private final Entry entry;
047  /** The virtual attribute provider for this virtual attribute. */
048  private final VirtualAttributeProvider<?> provider;
049  /** The virtual attribute rule for this virtual attribute. */
050  private final VirtualAttributeRule rule;
051
052
053
054  /**
055   * Creates a new virtual attribute with the provided information.
056   *
057   * @param attributeType
058   *          The attribute type for this virtual attribute.
059   * @param entry
060   *          The entry in which this virtual attribute exists.
061   * @param rule
062   *          The virtual attribute rule that governs the behavior of
063   *          this virtual attribute.
064   */
065  public VirtualAttribute(AttributeType attributeType, Entry entry,
066      VirtualAttributeRule rule)
067  {
068    this.attributeDescription = AttributeDescription.create(attributeType);
069    this.entry = entry;
070    this.rule = rule;
071    this.provider = rule.getProvider();
072  }
073
074  @Override
075  public ConditionResult approximatelyEqualTo(ByteString assertionValue)
076  {
077    return provider.approximatelyEqualTo(entry, rule, assertionValue);
078  }
079
080  @Override
081  public boolean contains(ByteString value)
082  {
083    return provider.hasValue(entry, rule, value);
084  }
085
086  @Override
087  public boolean containsAll(Collection<?> values)
088  {
089    return provider.hasAllValues(entry, rule, values);
090  }
091
092  @Override
093  public ConditionResult matchesEqualityAssertion(ByteString assertionValue)
094  {
095    return provider.matchesEqualityAssertion(entry, rule, assertionValue);
096  }
097
098  @Override
099  public AttributeDescription getAttributeDescription()
100  {
101    return attributeDescription;
102  }
103
104  /**
105   * Retrieves the virtual attribute rule that governs the behavior of
106   * this virtual attribute.
107   *
108   * @return The virtual attribute rule that governs the behavior of
109   *         this virtual attribute.
110   */
111  public VirtualAttributeRule getVirtualAttributeRule()
112  {
113    return rule;
114  }
115
116  @Override
117  public ConditionResult greaterThanOrEqualTo(ByteString assertionValue)
118  {
119    return provider.greaterThanOrEqualTo(entry, rule, assertionValue);
120  }
121
122  @Override
123  public boolean isEmpty()
124  {
125    return !provider.hasValue(entry, rule);
126  }
127
128  @Override
129  public boolean isVirtual()
130  {
131    return true;
132  }
133
134  @Override
135  public Iterator<ByteString> iterator()
136  {
137    return provider.getValues(entry, rule).iterator();
138  }
139
140  @Override
141  public ConditionResult lessThanOrEqualTo(ByteString assertionValue)
142  {
143    return provider.lessThanOrEqualTo(entry, rule, assertionValue);
144  }
145
146  @Override
147  public ConditionResult matchesSubstring(ByteString subInitial,
148      List<ByteString> subAny, ByteString subFinal)
149  {
150    return provider.matchesSubstring(entry, rule, subInitial, subAny, subFinal);
151  }
152
153  @Override
154  public int size()
155  {
156    if (provider.isMultiValued())
157    {
158      return provider.getValues(entry, rule).size();
159    }
160    return provider.hasValue(entry, rule) ? 1 : 0;
161  }
162
163  @Override
164  public void toString(StringBuilder buffer)
165  {
166    buffer.append("VirtualAttribute(");
167    buffer.append(getAttributeDescription().getAttributeType().getNameOrOID());
168    buffer.append(", {");
169    Utils.joinAsString(buffer, ", ", this);
170    buffer.append("})");
171  }
172}