View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.scriptbuilder.domain;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import java.util.List;
24  
25  import com.google.common.collect.ImmutableList;
26  import com.google.common.collect.ImmutableList.Builder;
27  
28  /**
29   * Statements used in a shell script
30   * 
31   * @author Adrian Cole
32   */
33  public class StatementList implements Statement {
34  
35     public final List<Statement> statements;
36  
37     public StatementList(Statement... statements) {
38        this.statements = ImmutableList.copyOf(checkNotNull(statements, "statements"));
39     }
40  
41     public StatementList(Iterable<Statement> statements) {
42        this.statements = ImmutableList.copyOf(checkNotNull(statements, "statements"));
43     }
44  
45     public String render(OsFamily family) {
46        StringBuilder statementsBuilder = new StringBuilder();
47        for (Statement statement : statements) {
48           statementsBuilder.append(statement.render(family));
49        }
50        return statementsBuilder.toString();
51     }
52  
53     @Override
54     public Iterable<String> functionDependencies(OsFamily family) {
55        Builder<String> functions = ImmutableList.<String> builder();
56        for (Statement statement : statements) {
57           functions.addAll(statement.functionDependencies(family));
58        }
59        return functions.build();
60     }
61  
62     @Override
63     public int hashCode() {
64        final int prime = 31;
65        int result = 1;
66        result = prime * result + ((statements == null) ? 0 : statements.hashCode());
67        return result;
68     }
69  
70     @Override
71     public boolean equals(Object obj) {
72        if (this == obj)
73           return true;
74        if (obj == null)
75           return false;
76        if (getClass() != obj.getClass())
77           return false;
78        StatementList other = (StatementList) obj;
79        if (statements == null) {
80           if (other.statements != null)
81              return false;
82        } else if (!statements.equals(other.statements))
83           return false;
84        return true;
85     }
86  
87     public List<Statement> getStatements() {
88        return statements;
89     }
90  }