PageRenderTime 27ms CodeModel.GetById 20ms 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
Possible License(s): Apache-2.0
  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. */
  16. package com.google.test.metric.cpp.dom;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. public class Variable {
  20. private final String name;
  21. private final List<String> path = new ArrayList<String>();
  22. private final String qualifiedName;
  23. public Variable(String name) {
  24. this.name = parse(name);
  25. this.qualifiedName = createQualifiedName();
  26. }
  27. public Variable(String name, Node context) {
  28. processContext(context);
  29. this.name = parse(name);
  30. this.qualifiedName = createQualifiedName();
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public String getQualifiedName() {
  36. return qualifiedName;
  37. }
  38. @Override
  39. public boolean equals(Object obj) {
  40. if (obj == this) {
  41. return true;
  42. }
  43. if (!(obj instanceof Variable)) {
  44. return false;
  45. }
  46. Variable that = (Variable) obj;
  47. return qualifiedName.equals(that.qualifiedName);
  48. }
  49. private void processContext(Node context) {
  50. while (context.getParent() != null) {
  51. if (context.getParent() instanceof Namespace) {
  52. Namespace namespace = (Namespace) context.getParent();
  53. if (namespace.getName() != null) {
  54. path.add(0, namespace.getName());
  55. }
  56. }
  57. context = context.getParent();
  58. }
  59. }
  60. private String createQualifiedName() {
  61. StringBuilder builder = new StringBuilder();
  62. for (String s : path) {
  63. builder.append(s);
  64. builder.append("::");
  65. }
  66. builder.append(name);
  67. return builder.toString();
  68. }
  69. private String parse(String qualifiedName) {
  70. String[] names = qualifiedName.split("::");
  71. for (int i = 0; i < names.length - 1; ++i) {
  72. path.add(names[i]);
  73. }
  74. return names[names.length - 1];
  75. }
  76. }