comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/GuardExpression.java @ 16759:23415229349b

Truffle-DSL: new package structure.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Aug 2014 15:57:14 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/typesystem/GuardExpression.java@bd28da642eea
children e6d15134ca86
comparison
equal deleted inserted replaced
16758:c5f8eeb3cbc8 16759:23415229349b
1 package com.oracle.truffle.dsl.processor.model;
2
3 import java.util.*;
4
5 public final class GuardExpression {
6
7 private GuardData resolvedGuard;
8
9 private final String guardName;
10 private final boolean negated;
11
12 public GuardExpression(String expression) {
13 if (expression.startsWith("!")) {
14 guardName = expression.substring(1, expression.length());
15 negated = true;
16 } else {
17 guardName = expression;
18 negated = false;
19 }
20 }
21
22 public boolean isResolved() {
23 return resolvedGuard != null;
24 }
25
26 public String getGuardName() {
27 return guardName;
28 }
29
30 public void setGuard(GuardData guard) {
31 this.resolvedGuard = guard;
32 }
33
34 @Override
35 public boolean equals(Object obj) {
36 if (obj instanceof GuardExpression) {
37 GuardExpression other = (GuardExpression) obj;
38 if (isResolved() && other.isResolved()) {
39 return resolvedGuard.equals(other.resolvedGuard) && negated == other.negated;
40 } else {
41 return guardName.equals(other.guardName) && negated == other.negated;
42 }
43 }
44 return false;
45 }
46
47 @Override
48 public int hashCode() {
49 return Objects.hash(guardName, negated, resolvedGuard);
50 }
51
52 public final boolean implies(GuardExpression other) {
53 if (other == this) {
54 return true;
55 }
56 if (getGuardName().equals(other.getGuardName())) {
57 if (isNegated() == other.isNegated()) {
58 return true;
59 }
60 }
61
62 if (isResolved() && other.isResolved()) {
63 for (GuardExpression implies : getResolvedGuard().getImpliesExpressions()) {
64 if (implies.getGuardName().equals(other.getGuardName())) {
65 if (implies.isNegated() == other.isNegated()) {
66 return true;
67 }
68 }
69 }
70 }
71 return false;
72 }
73
74 @Override
75 public String toString() {
76 return (negated ? "!" : "") + guardName;
77 }
78
79 public boolean isNegated() {
80 return negated;
81 }
82
83 public GuardData getResolvedGuard() {
84 return resolvedGuard;
85 }
86
87 }