View Javadoc

1   package org.apache.torque.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  
24  import org.apache.torque.Column;
25  
26  /**
27   * List with unique entries. UniqueList does not allow null nor will
28   * Columns with the same SQL expression be added twice.
29   *
30   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
31   * @version $Id: UniqueColumnList.java 1351125 2012-06-17 16:51:03Z tv $
32   */
33  public class UniqueColumnList extends ArrayList<Column>
34  {
35      /**
36       * Serial version
37       */
38      private static final long serialVersionUID = 4467847559423445120L;
39  
40      /**
41       * Constructs an empty UniqueList.
42       */
43      public UniqueColumnList()
44      {
45          // empty
46      }
47  
48      /**
49       * Copy-constructor. Creates a shallow copy of an UniqueList.
50       * @param list the uniqueList to copy
51       */
52      public UniqueColumnList(UniqueColumnList list)
53      {
54          this.addAll(list);
55      }
56  
57      /**
58       * Adds a Column to the list, if no column with the same SQL Expression
59       * is not already contained.
60       *
61       * @param column the Column to add, not null.
62       *
63       * @return true if the Object is added.
64       *
65       * @throws NullPointerException if column is null.
66       */
67      public boolean add(Column column)
68      {
69          if (column == null)
70          {
71              throw new NullPointerException("column must not be null");
72          }
73          if (!containsSqlExpression(column))
74          {
75              return super.add(column);
76          }
77          return false;
78      }
79  
80      /**
81       * Checks if this list already contains a column with the same
82       * SQL expression.
83       *
84       * @param column the column to check, not null.
85       *
86       * @return true if a column with the same Sql Expression is contained,
87       *         false otherwise.
88       */
89      public boolean containsSqlExpression(Column column)
90      {
91          for (Column candidate : this)
92          {
93              if (candidate.getSqlExpression().equals(
94                      column.getSqlExpression()))
95              {
96                  return true;
97              }
98          }
99          return false;
100     }
101 }