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                               QueryCommand definition ) {
48          super(name, columns);
49          this.definition = definition;
50      }
51  
52      protected ImmutableView( SelectorName name,
53                               Iterable<Column> columns,
54                               QueryCommand definition,
55                               Iterable<Column>... keyColumns ) {
56          super(name, columns, keyColumns);
57          this.definition = definition;
58      }
59  
60      protected ImmutableView( SelectorName name,
61                               Map<String, Column> columnsByName,
62                               List<Column> columns,
63                               QueryCommand definition,
64                               Set<Key> keys ) {
65          super(name, columnsByName, columns, keys);
66          this.definition = definition;
67      }
68  
69      /**
70       * {@inheritDoc}
71       * 
72       * @see org.modeshape.graph.query.validate.Schemata.View#getDefinition()
73       */
74      public QueryCommand getDefinition() {
75          return definition;
76      }
77  
78      /**
79       * {@inheritDoc}
80       * 
81       * @see java.lang.Object#toString()
82       */
83      @Override
84      public String toString() {
85          StringBuilder sb = new StringBuilder(getName().getName());
86          sb.append('(');
87          boolean first = true;
88          for (Column column : getColumns()) {
89              if (first) first = false;
90              else sb.append(", ");
91              sb.append(column);
92          }
93          sb.append(") AS '");
94          sb.append(Visitors.readable(definition));
95          sb.append('\'');
96          if (!getKeys().isEmpty()) {
97              sb.append(" with keys ");
98              first = true;
99              for (Key key : getKeys()) {
100                 if (first) first = false;
101                 else sb.append(", ");
102                 sb.append(key);
103             }
104         }
105         return sb.toString();
106     }
107 }