001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.naming.directory;
019:
020: import java.io.Serializable;
021:
022: import org.apache.harmony.jndi.internal.nls.Messages;
023:
024: /**
025: * This class is a combination of a modification code and attribute.
026: * <p>
027: * It is used by exception reporting (see
028: * <code>AttributeModificationException</code> for an example).
029: * </p>
030: * <p>
031: * The class is not thread-safe.
032: * </p>
033: */
034: public class ModificationItem implements Serializable {
035:
036: /*
037: * This constant is used during deserialization to check the version which
038: * created the serialized object
039: */
040: private static final long serialVersionUID = 0x69199e89ac11aae2L;
041:
042: /**
043: * Contains the modification to be performed.
044: *
045: * @serial
046: * @see DirContext
047: */
048: private int mod_op;
049:
050: /**
051: * The Attribute or value that is the source of the modification.
052: *
053: * @serial
054: */
055: private Attribute attr;
056:
057: /**
058: * Constructs a <code>ModificationItem</code> instance with all
059: * parameters.
060: *
061: * @param operation
062: * an operation code chosen from
063: * <code>DirContext.ADD_ATTRIBUTE</code>,
064: * <code>DirContext.REPLACE_ATTRIBUTE</code>,
065: * <code>DirContext.REMOVE_ATTRIBUTE</code>
066: * @param attribute
067: * the <code>Attribute</code> or value that is the source of
068: * the modification
069: */
070: public ModificationItem(int operation, Attribute attribute) {
071: if (null == attribute) {
072: // jndi.13=Non-null attribute is required for modification
073: throw new IllegalArgumentException(Messages
074: .getString("jndi.13")); //$NON-NLS-1$
075: }
076: if (!(DirContext.ADD_ATTRIBUTE == operation
077: || DirContext.REPLACE_ATTRIBUTE == operation || DirContext.REMOVE_ATTRIBUTE == operation)) {
078: // jndi.14=Modification code {0} must be one of
079: // DirContext.ADD_ATTRIBUTE, DirContext.REPLACE_ATTRIBUTE and
080: // DirContext.REMOVE_ATTRIBUTE
081: throw new IllegalArgumentException(Messages.getString(
082: "jndi.14", operation)); //$NON-NLS-1$
083: }
084: this .mod_op = operation;
085: this .attr = attribute;
086: }
087:
088: /**
089: * Gets the <code>Attribute</code> or value that is the source of the
090: * modification.
091: *
092: * @return the <code>Attribute</code> or value that is the source of the
093: * modification
094: */
095: public Attribute getAttribute() {
096: return this .attr;
097: }
098:
099: /**
100: * Gets the operation code.
101: *
102: * @return an operation code chosen from <code>
103: * DirContext.ADD_ATTRIBUTE</code>,
104: * <code>
105: * DirContext.REPLACE_ATTRIBUTE</code>, <code>
106: * DirContext.REMOVE_ATTRIBUTE</code>
107: */
108: public int getModificationOp() {
109: return this .mod_op;
110: }
111:
112: /**
113: * Returns string representations of this <code>ModificationItem</code>
114: * instance.
115: *
116: * @return a concatenation of string values for the operation and the
117: * attribute
118: */
119: @Override
120: public String toString() {
121: StringBuffer sb = new StringBuffer();
122: switch (mod_op) {
123: case DirContext.ADD_ATTRIBUTE:
124: sb.append("Operation is add attribute: "); //$NON-NLS-1$
125: break;
126: case DirContext.REMOVE_ATTRIBUTE:
127: sb.append("Operation is remove attribute: "); //$NON-NLS-1$
128: break;
129: case DirContext.REPLACE_ATTRIBUTE:
130: sb.append("Operation is replace attribute: "); //$NON-NLS-1$
131: break;
132: }
133: return sb.append(attr.toString()).toString();
134: }
135: }
|