PageRenderTime 34ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Java.NET/JavApi Commons collections (Apache Port)/org.apache.commons.collections.collection.TransformedCollection.cs

https://github.com/gadfly/nofs
C# | 129 lines | 48 code | 11 blank | 70 comment | 3 complexity | 7cc84cc4487eab138e7f24a919756bf4 MD5 | raw file
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. */
  15. using System;
  16. using java = biz.ritter.javapi;
  17. using org.apache.commons.collections;
  18. namespace org.apache.commons.collections.collection
  19. {
  20. /**
  21. * Decorates another <code>Collection</code> to transform objects that are added.
  22. * <p>
  23. * The add methods are affected by this class.
  24. * Thus objects must be removed or searched for using their transformed form.
  25. * For example, if the transformation converts Strings to Integers, you must
  26. * use the Integer form to remove objects.
  27. * <p>
  28. * This class is java.io.Serializable from Commons Collections 3.1.
  29. *
  30. * @since Commons Collections 3.0
  31. * @version $Revision$ $Date$
  32. *
  33. * @author Stephen Colebourne
  34. */
  35. [Serializable]
  36. public class TransformedCollection : AbstractSerializableCollectionDecorator
  37. {
  38. /** Serialization version */
  39. private static readonly long serialVersionUID = 8692300188161871514L;
  40. /** The transformer to use */
  41. protected readonly Transformer transformer;
  42. /**
  43. * Factory method to create a transforming collection.
  44. * <p>
  45. * If there are any elements already in the collection being decorated, they
  46. * are NOT transformed.
  47. *
  48. * @param coll the collection to decorate, must not be null
  49. * @param transformer the transformer to use for conversion, must not be null
  50. * @return a new transformed collection
  51. * @throws IllegalArgumentException if collection or transformer is null
  52. */
  53. public static java.util.Collection<Object> decorate(java.util.Collection<Object> coll, Transformer transformer)
  54. {
  55. return new TransformedCollection(coll, transformer);
  56. }
  57. //-----------------------------------------------------------------------
  58. /**
  59. * Constructor that wraps (not copies).
  60. * <p>
  61. * If there are any elements already in the collection being decorated, they
  62. * are NOT transformed.
  63. *
  64. * @param coll the collection to decorate, must not be null
  65. * @param transformer the transformer to use for conversion, must not be null
  66. * @throws IllegalArgumentException if collection or transformer is null
  67. */
  68. protected TransformedCollection(java.util.Collection<Object> coll, Transformer transformer)
  69. : base(coll)
  70. {
  71. if (transformer == null)
  72. {
  73. throw new java.lang.IllegalArgumentException("Transformer must not be null");
  74. }
  75. this.transformer = transformer;
  76. }
  77. /**
  78. * Transforms an object.
  79. * <p>
  80. * The transformer itself may throw an exception if necessary.
  81. *
  82. * @param object the object to transform
  83. * @return a transformed object
  84. */
  85. protected internal virtual Object transform(Object obj)
  86. {
  87. return transformer.transform(obj);
  88. }
  89. /**
  90. * Transforms a collection.
  91. * <p>
  92. * The transformer itself may throw an exception if necessary.
  93. *
  94. * @param coll the collection to transform
  95. * @return a transformed object
  96. */
  97. protected virtual java.util.Collection<Object> transform(java.util.Collection<Object> coll)
  98. {
  99. java.util.List<Object> list = new java.util.ArrayList<Object>(coll.size());
  100. for (java.util.Iterator<Object> it = coll.iterator(); it.hasNext(); )
  101. {
  102. list.add(transform(it.next()));
  103. }
  104. return list;
  105. }
  106. //-----------------------------------------------------------------------
  107. public override bool add(Object obj)
  108. {
  109. obj = transform(obj);
  110. return getCollection().add(obj);
  111. }
  112. public override bool addAll(java.util.Collection<Object> coll)
  113. {
  114. coll = transform(coll);
  115. return getCollection().addAll(coll);
  116. }
  117. }
  118. }