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.List;
27  import java.util.Map;
28  import java.util.Set;
29  import net.jcip.annotations.Immutable;
30  import org.modeshape.graph.query.model.QueryCommand;
31  import org.modeshape.graph.query.model.SelectorName;
32  import org.modeshape.graph.query.model.Visitors;
33  import org.modeshape.graph.query.validate.Schemata.Column;
34  import org.modeshape.graph.query.validate.Schemata.Key;
35  import org.modeshape.graph.query.validate.Schemata.View;
36  
37  /**
38   * 
39   */
40  @Immutable
41  class ImmutableView extends ImmutableTable implements View {
42  
43      private final QueryCommand definition;
44  
45      protected ImmutableView( SelectorName name,
46                               Iterable<Column> columns,
47                               boolean extraColumns,
48                               QueryCommand definition ) {
49          super(name, columns, extraColumns);
50          this.definition = definition;
51      }
52  
53      protected ImmutableView( SelectorName name,
54                               Iterable<Column> columns,
55                               boolean extraColumns,
56                               QueryCommand definition,
57                               Iterable<Column>... keyColumns ) {
58          super(name, columns, extraColumns, keyColumns);
59          this.definition = definition;
60      }
61  
62      protected ImmutableView( SelectorName name,
63                               Map<String, Column> columnsByName,
64                               List<Column> columns,
65                               boolean extraColumns,
66                               QueryCommand definition,
67                               Set<Key> keys ) {
68          super(name, columnsByName, columns, keys, extraColumns, columnsByName, columns);
69          this.definition = definition;
70      }
71  
72      protected ImmutableView( SelectorName name,
73                               Map<String, Column> columnsByName,
74                               List<Column> columns,
75                               boolean extraColumns,
76                               QueryCommand definition,
77                               Set<Key> keys,
78                               Map<String, Column> selectStarColumnsByName,
79                               List<Column> selectStarColumns ) {
80          super(name, columnsByName, columns, keys, extraColumns, selectStarColumnsByName, selectStarColumns);
81          this.definition = definition;
82      }
83  
84      /**
85       * {@inheritDoc}
86       * 
87       * @see org.modeshape.graph.query.validate.Schemata.View#getDefinition()
88       */
89      public QueryCommand getDefinition() {
90          return definition;
91      }
92  
93      /**
94       * {@inheritDoc}
95       * 
96       * @see org.modeshape.graph.query.validate.ImmutableTable#withColumnNotInSelectStar(java.lang.String)
97       */
98      @Override
99      public ImmutableView withColumnNotInSelectStar( String name ) {
100         ImmutableTable result = super.withColumnNotInSelectStar(name);
101         if (result == this) return this;
102         return new ImmutableView(result.getName(), result.getColumnsByName(), result.getColumns(), result.hasExtraColumns(),
103                                  definition, result.getKeySet(), result.getSelectAllColumnsByName(), result.getSelectAllColumns());
104     }
105 
106     /**
107      * {@inheritDoc}
108      * 
109      * @see java.lang.Object#toString()
110      */
111     @Override
112     public String toString() {
113         StringBuilder sb = new StringBuilder(getName().name());
114         sb.append('(');
115         boolean first = true;
116         for (Column column : getColumns()) {
117             if (first) first = false;
118             else sb.append(", ");
119             sb.append(column);
120         }
121         sb.append(") AS '");
122         sb.append(Visitors.readable(definition));
123         sb.append('\'');
124         if (!getKeys().isEmpty()) {
125             sb.append(" with keys ");
126             first = true;
127             for (Key key : getKeys()) {
128                 if (first) first = false;
129                 else sb.append(", ");
130                 sb.append(key);
131             }
132         }
133         return sb.toString();
134     }
135 }