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 String getNameWithOptions() 100 { 101 return getName(); 102 } 103 104 @Override 105 public AttributeDescription getAttributeDescription() 106 { 107 return attributeDescription; 108 } 109 110 /** 111 * Retrieves the virtual attribute rule that governs the behavior of 112 * this virtual attribute. 113 * 114 * @return The virtual attribute rule that governs the behavior of 115 * this virtual attribute. 116 */ 117 public VirtualAttributeRule getVirtualAttributeRule() 118 { 119 return rule; 120 } 121 122 @Override 123 public ConditionResult greaterThanOrEqualTo(ByteString assertionValue) 124 { 125 return provider.greaterThanOrEqualTo(entry, rule, assertionValue); 126 } 127 128 @Override 129 public boolean hasOption(String option) 130 { 131 return false; 132 } 133 134 @Override 135 public boolean hasOptions() 136 { 137 return false; 138 } 139 140 @Override 141 public boolean isEmpty() 142 { 143 return !provider.hasValue(entry, rule); 144 } 145 146 @Override 147 public boolean isVirtual() 148 { 149 return true; 150 } 151 152 @Override 153 public Iterator<ByteString> iterator() 154 { 155 return provider.getValues(entry, rule).iterator(); 156 } 157 158 @Override 159 public ConditionResult lessThanOrEqualTo(ByteString assertionValue) 160 { 161 return provider.lessThanOrEqualTo(entry, rule, assertionValue); 162 } 163 164 @Override 165 public ConditionResult matchesSubstring(ByteString subInitial, 166 List<ByteString> subAny, ByteString subFinal) 167 { 168 return provider.matchesSubstring(entry, rule, subInitial, subAny, subFinal); 169 } 170 171 @Override 172 public int size() 173 { 174 if (provider.isMultiValued()) 175 { 176 return provider.getValues(entry, rule).size(); 177 } 178 return provider.hasValue(entry, rule) ? 1 : 0; 179 } 180 181 @Override 182 public void toString(StringBuilder buffer) 183 { 184 buffer.append("VirtualAttribute("); 185 buffer.append(getAttributeDescription().getAttributeType().getNameOrOID()); 186 buffer.append(", {"); 187 Utils.joinAsString(buffer, ", ", this); 188 buffer.append("})"); 189 } 190}