/testability-explorer/src/main/java/com/google/test/metric/Type.java

http://testability-explorer.googlecode.com/ · Java · 89 lines · 61 code · 13 blank · 15 comment · 8 complexity · 5a25b3fa4b388f4ef3920ce1cbf007e9 MD5 · raw file

  1. /*
  2. * Copyright 2007 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;
  17. public class Type {
  18. private final String name;
  19. private final String code;
  20. private final int array;
  21. protected Type() {
  22. this.name = null;
  23. this.code = null;
  24. this.array = 0;
  25. }
  26. protected Type(String name, String code) {
  27. this(0, name, code);
  28. }
  29. protected Type(int array, String name, String code) {
  30. this.array = array;
  31. this.name = name;
  32. if (name.contains(";")) {
  33. throw new IllegalArgumentException();
  34. }
  35. this.code = code;
  36. }
  37. @Override
  38. public String toString() {
  39. return name;
  40. }
  41. public String getCode() {
  42. return code;
  43. }
  44. public Type toArray() {
  45. return new Type(array + 1, name + "[]", "[" + code);
  46. }
  47. public boolean isPrimitive() {
  48. return code.length() == 1;
  49. }
  50. public boolean isObject() {
  51. return !isPrimitive();
  52. }
  53. public boolean isArray() {
  54. return array > 0;
  55. }
  56. @Override
  57. public int hashCode() {
  58. final int prime = 31;
  59. int result = 1;
  60. result = prime * result + array;
  61. result = prime * result + ((code == null) ? 0 : code.hashCode());
  62. result = prime * result + ((name == null) ? 0 : name.hashCode());
  63. return result;
  64. }
  65. @Override
  66. public boolean equals(Object obj) {
  67. if (this == obj) {
  68. return true;
  69. }
  70. if (obj == null) {
  71. return false;
  72. }
  73. Type other = (Type) obj;
  74. return code.equals(other.code);
  75. }
  76. }