PageRenderTime 43ms CodeModel.GetById 36ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/cpp/src/main/java/com/google/test/metric/cpp/dom/Variable.java

http://testability-explorer.googlecode.com/
Java | 86 lines | 61 code | 10 blank | 15 comment | 10 complexity | e32c510729a8dfe4d3d5098e6f62337e MD5 | raw file
 1/*
 2 * Copyright 2008 Google Inc.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5 * use this file except in compliance with the License. You may obtain a copy of
 6 * the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package com.google.test.metric.cpp.dom;
17
18import java.util.ArrayList;
19import java.util.List;
20
21public class Variable {
22  private final String name;
23  private final List<String> path = new ArrayList<String>();
24  private final String qualifiedName;
25
26  public Variable(String name) {
27    this.name = parse(name);
28    this.qualifiedName = createQualifiedName();
29  }
30
31  public Variable(String name, Node context) {
32    processContext(context);
33    this.name = parse(name);
34    this.qualifiedName = createQualifiedName();
35  }
36
37  public String getName() {
38    return name;
39  }
40
41  public String getQualifiedName() {
42    return qualifiedName;
43  }
44
45  @Override
46  public boolean equals(Object obj) {
47    if (obj == this) {
48      return true;
49    }
50    if (!(obj instanceof Variable)) {
51      return false;
52    }
53    Variable that = (Variable) obj;
54    return qualifiedName.equals(that.qualifiedName);
55  }
56
57  private void processContext(Node context) {
58    while (context.getParent() != null) {
59      if (context.getParent() instanceof Namespace) {
60        Namespace namespace = (Namespace) context.getParent();
61        if (namespace.getName() != null) {
62          path.add(0, namespace.getName());
63        }
64      }
65      context = context.getParent();
66    }
67  }
68
69  private String createQualifiedName() {
70    StringBuilder builder = new StringBuilder();
71    for (String s : path) {
72      builder.append(s);
73      builder.append("::");
74    }
75    builder.append(name);
76    return builder.toString();
77  }
78
79  private String parse(String qualifiedName) {
80    String[] names = qualifiedName.split("::");
81    for (int i = 0; i < names.length - 1; ++i) {
82      path.add(names[i]);
83    }
84    return names[names.length - 1];
85  }
86}