View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.query.validate;
25  
26  import java.util.Collections;
27  import java.util.HashSet;
28  import java.util.Set;
29  import org.modeshape.graph.query.validate.Schemata.Column;
30  import org.modeshape.graph.query.validate.Schemata.Key;
31  
32  /**
33   * 
34   */
35  public class ImmutableKey implements Key {
36  
37      private final Set<Column> columns;
38  
39      protected ImmutableKey( Iterable<Column> columns ) {
40          assert columns != null;
41          Set<Column> columnSet = new HashSet<Column>();
42          for (Column column : columns) {
43              if (column != null) columnSet.add(column);
44          }
45          assert !columnSet.isEmpty();
46          this.columns = Collections.unmodifiableSet(columnSet);
47      }
48  
49      protected ImmutableKey( Column... columns ) {
50          assert columns != null;
51          assert columns.length > 0;
52          Set<Column> columnSet = new HashSet<Column>();
53          for (Column column : columns) {
54              if (column != null) columnSet.add(column);
55          }
56          assert !columnSet.isEmpty();
57          this.columns = Collections.unmodifiableSet(columnSet);
58      }
59  
60      /**
61       * {@inheritDoc}
62       * 
63       * @see org.modeshape.graph.query.validate.Schemata.Key#getColumns()
64       */
65      public Set<Column> getColumns() {
66          return columns;
67      }
68  
69      /**
70       * {@inheritDoc}
71       * 
72       * @see org.modeshape.graph.query.validate.Schemata.Key#hasColumns(org.modeshape.graph.query.validate.Schemata.Column[])
73       */
74      public boolean hasColumns( Column... columns ) {
75          Set<Column> keyColumns = new HashSet<Column>(this.columns);
76          for (Column expected : columns) {
77              if (!keyColumns.remove(expected)) return false;
78          }
79          return keyColumns.isEmpty();
80      }
81  
82      /**
83       * {@inheritDoc}
84       * 
85       * @see org.modeshape.graph.query.validate.Schemata.Key#hasColumns(java.lang.Iterable)
86       */
87      public boolean hasColumns( Iterable<Column> columns ) {
88          Set<Column> keyColumns = new HashSet<Column>(this.columns);
89          for (Column expected : columns) {
90              if (!keyColumns.remove(expected)) return false;
91          }
92          return keyColumns.isEmpty();
93      }
94  
95      /**
96       * {@inheritDoc}
97       * 
98       * @see java.lang.Object#toString()
99       */
100     @Override
101     public String toString() {
102         StringBuilder sb = new StringBuilder();
103         boolean first = true;
104         sb.append('[');
105         for (Column column : columns) {
106             if (first) first = false;
107             else sb.append(", ");
108             sb.append(column);
109         }
110         sb.append(']');
111         return sb.toString();
112     }
113 }