1. /*
  2. * @(#)ModificationItem.java 1.7 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.naming.directory;
  8. /**
  9. * This class represents a modification item.
  10. * It consists of a modification code and an attribute on which to operate.
  11. *<p>
  12. * A ModificationItem instance is not synchronized against concurrent
  13. * multithreaded access. Multiple threads trying to access and modify
  14. * a single ModificationItem instance should lock the object.
  15. *
  16. * @author Rosanna Lee
  17. * @author Scott Seligman
  18. * @version 1.7 03/01/23
  19. * @since 1.3
  20. */
  21. /*
  22. *<p>
  23. * The serialized form of a ModificationItem object consists of the
  24. * modification op (and int) and the corresponding Attribute.
  25. */
  26. public class ModificationItem implements java.io.Serializable {
  27. /**
  28. * Contains an integer identify the modification
  29. * to be performed.
  30. * @serial
  31. */
  32. private int mod_op;
  33. /**
  34. * Contains the attribute identifying
  35. * the attribute and/or its value to be applied for the modification.
  36. * @serial
  37. */
  38. private Attribute attr;
  39. /**
  40. * Creates a new instance of ModificationItem.
  41. * @param mod_op Modification to apply. It must be one of:
  42. * DirContext.ADD_ATTRIBUTE
  43. * DirContext.REPLACE_ATTRIBUTE
  44. * DirContext.REMOVE_ATTRIBUTE
  45. * @param attr The non-null attribute to use for modification.
  46. * @exception IllegalArgumentException If attr is null, or if mod_op is
  47. * not one of the ones specified above.
  48. */
  49. public ModificationItem(int mod_op, Attribute attr) {
  50. switch (mod_op) {
  51. case DirContext.ADD_ATTRIBUTE:
  52. case DirContext.REPLACE_ATTRIBUTE:
  53. case DirContext.REMOVE_ATTRIBUTE:
  54. if (attr == null)
  55. throw new IllegalArgumentException("Must specify non-null attribute for modification");
  56. this.mod_op = mod_op;
  57. this.attr = attr;
  58. break;
  59. default:
  60. throw new IllegalArgumentException("Invalid modification code " + mod_op);
  61. }
  62. }
  63. /**
  64. * Retrieves the modification code of this modification item.
  65. * @return The modification code. It is one of:
  66. * DirContext.ADD_ATTRIBUTE
  67. * DirContext.REPLACE_ATTRIBUTE
  68. * DirContext.REMOVE_ATTRIBUTE
  69. */
  70. public int getModificationOp() {
  71. return mod_op;
  72. }
  73. /**
  74. * Retrieves the attribute associated with this modification item.
  75. * @return The non-null attribute to use for the modification.
  76. */
  77. public Attribute getAttribute() {
  78. return attr;
  79. }
  80. /**
  81. * Generates the string representation of this modification item,
  82. * which consists of the modification operation and its related attribute.
  83. * The string representation is meant for debugging and not to be
  84. * interpreted programmatically.
  85. *
  86. * @return The non-null string representation of this modification item.
  87. */
  88. public String toString() {
  89. switch (mod_op) {
  90. case DirContext.ADD_ATTRIBUTE:
  91. return ("Add attribute: " + attr.toString());
  92. case DirContext.REPLACE_ATTRIBUTE:
  93. return ("Replace attribute: " + attr.toString());
  94. case DirContext.REMOVE_ATTRIBUTE:
  95. return ("Remove attribute: " + attr.toString());
  96. }
  97. return ""; // should never happen
  98. }
  99. /**
  100. * Use serialVersionUID from JNDI 1.1.1 for interoperability
  101. */
  102. private static final long serialVersionUID = 7573258562534746850L;
  103. }