PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 25ms app.codeStats 0ms

/src/org/ooc/frontend/model/VersionNodes.java

http://github.com/nddrylliog/ooc
Java | 225 lines | 163 code | 58 blank | 4 comment | 15 complexity | 8e57c523b0bdee11af8aa472173ffaf2 MD5 | raw file
  1package org.ooc.frontend.model;
  2
  3import java.io.IOException;
  4
  5import org.ooc.frontend.Target;
  6
  7public class VersionNodes {
  8
  9	public static interface VersionNodeVisitor {
 10
 11		void visit(VersionName versionName) throws IOException;
 12		void visit(VersionNegation versionNegation) throws IOException;
 13		void visit(VersionAnd versionAnd) throws IOException;
 14		void visit(VersionOr versionOr) throws IOException;
 15		void visit(VersionParen versionParen) throws IOException;
 16		
 17	}
 18	
 19	/**
 20	 * A version string, corresponding to a #define 
 21	 * @author Amos Wenger
 22	 */
 23	public static abstract class VersionNode {
 24		
 25		public abstract void accept(VersionNodeVisitor visitor) throws IOException;
 26		public abstract void acceptChildren(VersionNodeVisitor visitor) throws IOException;
 27		public abstract boolean isSatisfied(Target target);
 28		
 29	}
 30	
 31	public static class VersionName extends VersionNode {
 32		
 33		String name;
 34		boolean solved = false;
 35		
 36		public VersionName(String name) {
 37			this.name = name;
 38		}
 39		
 40		public String getName() {
 41			return name;
 42		}
 43
 44		@Override
 45		public void accept(VersionNodeVisitor visitor) throws IOException {
 46			visitor.visit(this);
 47		}
 48
 49		@Override
 50		public void acceptChildren(VersionNodeVisitor visitor) {}
 51		
 52		@Override
 53		public String toString() {
 54			return name;
 55		}
 56
 57		@Override
 58		public boolean isSatisfied(Target target) {
 59			if(name.toLowerCase().contains("win") && target == Target.WIN) {
 60				return true;
 61			}
 62			if((name.toLowerCase().contains("linux") 
 63					|| name.toLowerCase().contains("unix")) && target == Target.LINUX) {
 64				return true;
 65			}
 66			if(name.toLowerCase().contains("apple") && target == Target.OSX) {
 67				return true;
 68			}
 69			if(name.toLowerCase().contains("sun") && target == Target.SOLARIS) {
 70				return true;
 71			}
 72			return false;
 73		}
 74		
 75	}
 76	
 77	public static class VersionNegation extends VersionNode {
 78	
 79		VersionNode inner;
 80
 81		public VersionNegation(VersionNode inner) {
 82			this.inner = inner;
 83		}
 84		
 85		public VersionNode getInner() {
 86			return inner;
 87		}
 88		
 89		@Override
 90		public void accept(VersionNodeVisitor visitor) throws IOException {
 91			visitor.visit(this);
 92		}
 93
 94		@Override
 95		public void acceptChildren(VersionNodeVisitor visitor) throws IOException {
 96			inner.accept(visitor);
 97		}
 98		
 99		@Override
100		public String toString() {
101			return "!" + inner;
102		}
103
104		@Override
105		public boolean isSatisfied(Target target) {
106			return !inner.isSatisfied(target);
107		}
108		
109	}
110	
111	public static class VersionParen extends VersionNode {
112		
113		VersionNode inner;
114
115		public VersionParen(VersionNode inner) {
116			this.inner = inner;
117		}
118		
119		public VersionNode getInner() {
120			return inner;
121		}
122		
123		@Override
124		public void accept(VersionNodeVisitor visitor) throws IOException {
125			visitor.visit(this);
126		}
127
128		@Override
129		public void acceptChildren(VersionNodeVisitor visitor) throws IOException {
130			inner.accept(visitor);
131		}
132		
133		@Override
134		public String toString() {
135			return "(" + inner + ")";
136		}
137		
138		@Override
139		public boolean isSatisfied(Target target) {
140			return inner.isSatisfied(target);
141		}
142		
143	}
144	
145	public abstract static class VersionCouple extends VersionNode {
146	
147		VersionNode left;
148		VersionNode right;
149		
150		public VersionCouple(VersionNode left, VersionNode right) {
151			this.left = left;
152			this.right = right;
153		}
154		
155		public VersionNode getLeft() {
156			return left;
157		}
158		
159		public void setLeft(VersionNode left) {
160			this.left = left;
161		}
162		
163		public VersionNode getRight() {
164			return right;
165		}
166		
167		public void setRight(VersionNode right) {
168			this.right = right;
169		}
170		
171		@Override
172		public void acceptChildren(VersionNodeVisitor visitor) throws IOException {
173			left.accept(visitor);
174			right.accept(visitor);
175		}
176		
177	}
178	
179	public static class VersionAnd extends VersionCouple {
180
181		public VersionAnd(VersionNode left, VersionNode right) {
182			super(left, right);
183		}
184
185		@Override
186		public void accept(VersionNodeVisitor visitor) throws IOException {
187			visitor.visit(this);
188		}
189
190		@Override
191		public String toString() {
192			return left + " && " + right;
193		}
194		
195		@Override
196		public boolean isSatisfied(Target target) {
197			return left.isSatisfied(target) && right.isSatisfied(target);
198		}
199
200	}
201	
202	public static class VersionOr extends VersionCouple {
203
204		public VersionOr(VersionNode left, VersionNode right) {
205			super(left, right);
206		}
207
208		@Override
209		public void accept(VersionNodeVisitor visitor) throws IOException {
210			visitor.visit(this);
211		}
212		
213		@Override
214		public String toString() {
215			return left + " || " + right;
216		}
217		
218		@Override
219		public boolean isSatisfied(Target target) {
220			return left.isSatisfied(target) || right.isSatisfied(target);
221		}
222		
223	}
224	
225}