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-2009 Sun Microsystems, Inc. 025 * Portions Copyright 2012-2015 ForgeRock AS. 026 */ 027package org.opends.server.types; 028 029import java.util.Collection; 030import java.util.Collections; 031import java.util.Iterator; 032import java.util.List; 033import java.util.Set; 034 035import org.forgerock.i18n.slf4j.LocalizedLogger; 036import org.forgerock.opendj.ldap.ByteString; 037import org.forgerock.opendj.ldap.ConditionResult; 038import org.forgerock.util.Utils; 039import org.opends.server.api.VirtualAttributeProvider; 040 041/** 042 * This class defines a virtual attribute, which is a special kind of 043 * attribute whose values do not actually exist in persistent storage 044 * but rather are computed or otherwise obtained dynamically. 045 */ 046@org.opends.server.types.PublicAPI( 047 stability = org.opends.server.types.StabilityLevel.VOLATILE, 048 mayInstantiate = false, 049 mayExtend = false, 050 mayInvoke = true) 051public final class VirtualAttribute 052 extends AbstractAttribute 053{ 054 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 055 056 /** The attribute type. */ 057 private final AttributeType attributeType; 058 /** The entry with which this virtual attribute is associated. */ 059 private final Entry entry; 060 /** The virtual attribute provider for this virtual attribute. */ 061 private final VirtualAttributeProvider<?> provider; 062 /** The virtual attribute rule for this virtual attribute. */ 063 private final VirtualAttributeRule rule; 064 065 066 067 /** 068 * Creates a new virtual attribute with the provided information. 069 * 070 * @param attributeType 071 * The attribute type for this virtual attribute. 072 * @param entry 073 * The entry in which this virtual attribute exists. 074 * @param rule 075 * The virtual attribute rule that governs the behavior of 076 * this virtual attribute. 077 */ 078 public VirtualAttribute(AttributeType attributeType, Entry entry, 079 VirtualAttributeRule rule) 080 { 081 this.attributeType = attributeType; 082 this.entry = entry; 083 this.rule = rule; 084 this.provider = rule.getProvider(); 085 } 086 087 @Override 088 public ConditionResult approximatelyEqualTo(ByteString assertionValue) 089 { 090 return provider.approximatelyEqualTo(entry, rule, assertionValue); 091 } 092 093 @Override 094 public boolean contains(ByteString value) 095 { 096 return provider.hasValue(entry, rule, value); 097 } 098 099 @Override 100 public boolean containsAll(Collection<ByteString> values) 101 { 102 return provider.hasAllValues(entry, rule, values); 103 } 104 105 @Override 106 public ConditionResult matchesEqualityAssertion(ByteString assertionValue) 107 { 108 return provider.matchesEqualityAssertion(entry, rule, assertionValue); 109 } 110 111 @Override 112 public AttributeType getAttributeType() 113 { 114 return attributeType; 115 } 116 117 @Override 118 public String getNameWithOptions() 119 { 120 return getName(); 121 } 122 123 @Override 124 public Set<String> getOptions() 125 { 126 return Collections.emptySet(); 127 } 128 129 130 131 /** 132 * Retrieves the virtual attribute rule that governs the behavior of 133 * this virtual attribute. 134 * 135 * @return The virtual attribute rule that governs the behavior of 136 * this virtual attribute. 137 */ 138 public VirtualAttributeRule getVirtualAttributeRule() 139 { 140 return rule; 141 } 142 143 @Override 144 public ConditionResult greaterThanOrEqualTo(ByteString assertionValue) 145 { 146 return provider.greaterThanOrEqualTo(entry, rule, assertionValue); 147 } 148 149 @Override 150 public boolean hasAllOptions(Collection<String> options) 151 { 152 return options == null || options.isEmpty(); 153 } 154 155 @Override 156 public boolean hasOption(String option) 157 { 158 return false; 159 } 160 161 @Override 162 public boolean hasOptions() 163 { 164 return false; 165 } 166 167 @Override 168 public boolean isEmpty() 169 { 170 return !provider.hasValue(entry, rule); 171 } 172 173 @Override 174 public boolean isVirtual() 175 { 176 return true; 177 } 178 179 @Override 180 public Iterator<ByteString> iterator() 181 { 182 return provider.getValues(entry, rule).iterator(); 183 } 184 185 @Override 186 public ConditionResult lessThanOrEqualTo(ByteString assertionValue) 187 { 188 return provider.lessThanOrEqualTo(entry, rule, assertionValue); 189 } 190 191 @Override 192 public ConditionResult matchesSubstring(ByteString subInitial, 193 List<ByteString> subAny, ByteString subFinal) 194 { 195 return provider.matchesSubstring(entry, rule, subInitial, subAny, subFinal); 196 } 197 198 @Override 199 public boolean optionsEqual(Set<String> options) 200 { 201 return options == null || options.isEmpty(); 202 } 203 204 @Override 205 public int size() 206 { 207 if (provider.isMultiValued()) 208 { 209 return provider.getValues(entry, rule).size(); 210 } 211 return provider.hasValue(entry, rule) ? 1 : 0; 212 } 213 214 @Override 215 public void toString(StringBuilder buffer) 216 { 217 buffer.append("VirtualAttribute("); 218 buffer.append(getAttributeType().getNameOrOID()); 219 buffer.append(", {"); 220 Utils.joinAsString(buffer, ", ", this); 221 buffer.append("})"); 222 } 223}