/** * Détermine si la forme engendrée par les 3 cases représente un bateau correct ou non * * @param lc * @return */ private boolean isBoatCorrect(LinkedList<Case> lc) { if (lc.size() != 3) return false; // Order the list LinkedList<Case> nlc = new LinkedList<Case>(); for (Entry<String, Integer> en : this.matchKeys.entrySet()) for (Case c : lc) if (c.getName().equals(en.getKey())) nlc.add(c); String firstCase = nlc.get(0).getName(); String secondCase = nlc.get(1).getName(); String thirdCase = nlc.get(2).getName(); List<String[]> rows = new ArrayList<String[]>(); rows.add(this.numbersRowTitles); rows.add(this.firstRowTitles); rows.add(this.secondRowTitles); rows.add(this.thirdRowTitles); for (int i = 0; i < rows.size(); i++) { if (getIndexOf(rows.get(i), firstCase) != -1) return this.checkRow(rows, i, firstCase, secondCase, thirdCase); } return false; }
private Type typecheckCases(Environment env, Optional<Type> expected) { Type commonType = null; // do actual type-checking on cases for (Case c : cases) { Type rt = c.getAST().typecheck(env, expected); // System.out.println("rt = " + rt); if (commonType == null) commonType = rt; if (!rt.equals(commonType)) { // System.out.println("WARNING: rt = " + rt + " and commonType = " + commonType); ToolError.reportError(ErrorMessage.MATCH_NO_COMMON_RETURN, this); return null; } } if (expected.equals(Optional.empty())) { return commonType; } else { // System.out.println("expected = " + expected); // System.out.println("commonType = " + commonType); if (commonType == null || /*expected.equals(commonType) ||*/ expected.get().equals(commonType)) { return commonType; } else { ToolError.reportError(ErrorMessage.MATCH_NO_COMMON_RETURN, this); return null; } } }
public void resolve(Environment env) { if (this.defaultCase != null) this.defaultCase.resolve(env, this); for (Case c : this.cases) { c.resolve(env, this); } }
private int setCase(int x, int y, Batiment bat, int taillex, int tailley) { int axe, xtemp, ytemp; Case cas; for (int i = 0; i < taillex; i++) { for (int j = 0; j < tailley; j++) { xtemp = (x + i); ytemp = (y + j) * this.nbCasesLignes; axe = xtemp + ytemp; cas = this.grille.get(axe); if (cas.getStatut() != 0) { return -1; } } } for (int i = 0; i < taillex; i++) { for (int j = 0; j < tailley; j++) { xtemp = (x + i); ytemp = (y + j) * this.nbCasesLignes; axe = xtemp + ytemp; cas = this.grille.get(axe); cas.setOccuper(bat); } } return 0; }
public Color getCaseColor(int x, int y) { int axe; y = y * this.nbCasesLignes; axe = y + x; Case cas = this.grille.get(axe); return cas.getColor(); }
/** * Helper method to simplify checking for a tag. Returns true if the given binding tag is present * in the list of cases. * * @param cases * @param binding * @return */ private boolean containsTagBinding(List<Case> cases, Type tagName) { for (Case c : cases) { // Found a match, this tag is present if (c.getTaggedTypeMatch().equals(tagName)) return true; } return false; }
@Test public void testMerge() { val cases1 = Cases.of(Case.of(input -> true, input -> "hello")); val cases2 = Cases.of(Case.of(input -> true, input -> "hello")); val cases3 = cases1.merge(cases2); assertThat(cases3.size(), is(cases1.size() + cases2.size())); }
@Test public void testFilterFunction() { val cases = Cases.of( Case.of(input -> true, input -> "hello"), Case.of(input -> false, input -> "second")) .filterFunction(fn -> fn.apply(10).equals("second")); assertThat(cases.size(), is(1)); }
@Test public void testFilterPredicate() { val cases = Cases.of( Case.of(input -> true, input -> "hello"), Case.of(input -> false, input -> "second")) .filterPredicate(p -> p.test(10)); assertThat(cases.size(), is(1)); }
/** Lie les cases avec les IHMCase (case graphique JButton) */ public void lierCaseInterface() { for (int i = 0; i < TaillePlateauX; i++) { for (int j = 0; j < TaillePlateauY; j++) { int[] positionCaseSelectionné = {i, j}; Case c = plateau.getCase(positionCaseSelectionné); IHMCase ie = ihmjeu.getGuiCase(positionCaseSelectionné); c.addObserver(ie); } } }
@Test public void testMatchObjectArray() { assertThat( Cases.of( Case.of((List<Integer> input) -> input.size() == 3, input -> "hello"), Case.of((List<Integer> input) -> input.size() == 2, input -> "ignored"), Case.of((List<Integer> input) -> input.size() == 1, input -> "world")) .match(1, 2, 3) .get(), is("hello")); }
@Test public void testFlatMapAll() { val cases = Cases.of( Case.of(input -> true, input -> "hello"), Case.of(input -> false, input -> "second")) .flatMapAll( input -> (Cases) Cases.ofPStack(input.plus(Case.of(in -> true, in -> "new")))); assertThat(cases.size(), is(3)); }
protected Element createCaseElement(Document doc, Case c) { Element elem = createElement(doc, CASE_NAME, null, null); int[] decomposition = c.getInput(); elem.appendChild(createElement(doc, CASE_U_NAME, null, "" + decomposition[0])); elem.appendChild(createElement(doc, CASE_V_NAME, null, "" + decomposition[1])); elem.appendChild(createElement(doc, CASE_X_NAME, null, "" + decomposition[2])); elem.appendChild(createElement(doc, CASE_Y_NAME, null, "" + decomposition[3])); elem.appendChild(createElement(doc, CASE_I_NAME, null, "" + c.getI())); return elem; }
@Test public void testUnapply() { assertThat( Cases.of( Case.of((List input) -> input.size() == 3, input -> "hello"), Case.of((List input) -> input.size() == 2, input -> "ignored"), Case.of((List input) -> input.size() == 1, input -> "world")) .unapply(new MyClass(1, "hello")) .get(), is("ignored")); }
@Test public void testMapPredicate() { List results = Cases.of( Case.of(input -> true, input -> "hello"), Case.of(input -> false, input -> "second")) .mapPredicate(p -> input -> true) .matchMany(10) .collect(Collectors.toList()); assertThat(results.size(), is(2)); }
private Case findRoute(Request request) throws Exception { for (Case r : cases) { // TODO getBodyAsStream creates ByteArray each call. That could be a performance issue. Using // BufferedInputStream did't work, because stream got closed. InputSource is = new InputSource(request.getBodyAsStreamDecoded()); is.setEncoding(request.getCharset()); if ((Boolean) newXPath(namespaces).evaluate(r.getxPath(), is, XPathConstants.BOOLEAN)) return r; log.debug("no match found for xpath {" + r.getxPath() + "}"); } return null; }
@Test public void testMatchAsync() { assertThat( Cases.of( Case.of((List<Integer> input) -> input.size() == 3, input -> "hello"), Case.of((List<Integer> input) -> input.size() == 2, input -> "ignored"), Case.of((List<Integer> input) -> input.size() == 1, input -> "world")) .matchAsync(ForkJoinPool.commonPool(), 1, 2, 3) .join() .get(), is("hello")); }
private void checkAllCasesAreUnique() { // All tagged types must be unique Set<Type> caseSet = new HashSet<Type>(); for (Case c : cases) { if (c.isTyped()) caseSet.add(c.getTaggedTypeMatch()); } if (caseSet.size() != cases.size()) { ToolError.reportError(ErrorMessage.DUPLICATE_TAG, matchingOver); } }
/** * Cherche les rois sur l'echiquier * * @return */ public void findKings() { for (int i = 0; i < plateau.size(); i++) { if (!plateau.get(i).estVide()) if (plateau.get(i).getPiece().getClass().getSimpleName().equals(new String("Roi"))) if (plateau.get(i).getPiece().getCamp().equals("noir")) { caseRoiNoir.setPosition(plateau.get(i).getPosition()); caseRoiNoir.setPiece(new Roi("noir")); } else if (plateau.get(i).getPiece().getCamp().equals("blanc")) { caseRoiBlanc.setPosition(plateau.get(i).getPosition()); caseRoiBlanc.setPiece(new Roi("blanc")); } } }
/** * Checks that the tag we are matching over is a supertag of every tag in the case-list. * * <p>This ensures that each tag could actually have a match and that case is not unreachable * code. * * @param matchingOverTag */ private void checkStaticSubtags(TaggedInfo matchingOver) { for (Case c : cases) { TaggedInfo matchTarget = TaggedInfo.lookupTagByType(c.getTaggedTypeMatch()); // FIXME: if (!isSubtag(matchTarget, matchingOver)) { ToolError.reportError( ErrorMessage.UNMATCHABLE_CASE, this.matchingOver, matchingOver.getTagName(), matchTarget.getTagName()); } } }
/** Checks there is not more than one default. */ private void checkNotMultipleDefaults() { for (int numDefaults = 0, i = 0; i < originalCaseList.size(); i++) { Case c = originalCaseList.get(i); if (c.isDefault()) { numDefaults++; if (numDefaults > 1) { ToolError.reportError(ErrorMessage.MULTIPLE_DEFAULTS, matchingOver); } } } }
@Test public void testMatchManyFromStream() { List<String> results = Cases.of( Case.of((Integer input) -> 10 == input, input -> "hello"), Case.of(input -> 11 == input, input -> "world")) .<String>matchManyFromStream(Stream.of(1, 10, 11)) .collect(Collectors.toList()); assertThat(results.size(), is(2)); assertThat(results, hasItem("hello")); assertThat(results, hasItem("world")); }
public String getCanonicalForm() { StringBuilder buf = new StringBuilder("<#switch "); buf.append(testExpression.getCanonicalForm()); buf.append(">"); for (int i = 0; i < nestedElements.size(); i++) { Case cas = (Case) nestedElements.get(i); buf.append(cas.getCanonicalForm()); } if (defaultCase != null) { buf.append(defaultCase.getCanonicalForm()); } buf.append("</#switch>"); return buf.toString(); }
@Test public void testMapFunction() { List<String> results = Cases.of( Case.of(input -> true, input -> "hello"), Case.of(input -> true, input -> "second")) .mapFunction(fn -> input -> "prefix_" + fn.apply(input)) .<String>matchMany(10) .collect(Collectors.toList()); assertThat(results.size(), is(2)); assertTrue(results.stream().allMatch(s -> s.startsWith("prefix_"))); assertTrue(results.stream().anyMatch(s -> s.startsWith("prefix_hello"))); assertTrue(results.stream().anyMatch(s -> s.startsWith("prefix_second"))); }
/** * Filtre la liste selon la presence d'une pièce du meme camp * * @param camp camp de la piece de la case de depart * @param casePossible Liste des cases possibles, a filtrer * @return Liste filtree */ private ArrayList<Case> filtrerDeplacementPossible(String camp, ArrayList<Case> casePossible) { Case caseplateau; for (int i = 0; i < (casePossible.size()); i++) { caseplateau = chercherCase(casePossible.get(i).getPosition()); if ((!caseplateau.estVide())) { if (caseplateau.getPiece().getCamp().equals(camp)) { casePossible.remove(casePossible.get(i)); i--; } } } return casePossible; }
@Test public void testAppend() { Cases<Integer, String, Function<Integer, String>> cases1 = Cases.of( Case.of((Integer input) -> 10 == input, input -> "hello"), Case.of(input -> 11 == input, input -> "world")); Case<Integer, String, Function<Integer, String>> caze = Case.of((Integer input) -> 11 == input, input -> "hello"); val cases3 = cases1.append(1, caze); assertThat(cases3.size(), is(3)); assertThat(cases3.match(11).get(), is("hello")); }
@Override public Map<String, TypedAST> getChildren() { Map<String, TypedAST> children = new HashMap<>(); for (Case c : cases) { // is there a proper convention for names in children? children.put("match case: " + c.getTaggedTypeMatch(), c.getAST()); } if (defaultCase != null) { children.put("match default-case: " + defaultCase.getTaggedTypeMatch(), defaultCase.getAST()); } return children; }
@Test public void testMatchFromStreamAsync() { List<String> results = Cases.of( Case.of((Integer input) -> 10 == input, input -> "hello"), Case.of((Integer input) -> 10 == input, input -> "ignored"), Case.of(input -> 11 == input, input -> "world")) .<String>matchFromStreamAsync(ForkJoinPool.commonPool(), Stream.of(1, 11, 10)) .join() .collect(Collectors.toList()); assertThat(results.size(), is(2)); assertThat(results, hasItem("hello")); assertThat(results, hasItem("world")); }
@Override public Outcome handleRequest(Exchange exc) throws Exception { if (exc.getRequest().isBodyEmpty()) { return Outcome.CONTINUE; } Case r = findRoute(exc.getRequest()); if (r == null) { return Outcome.CONTINUE; } log.debug("match found for {" + r.getxPath() + "} routing to {" + r.getUrl() + "}"); updateDestination(exc, r); return Outcome.CONTINUE; }
/** * regarde si la case e est menace par le camp c * * @param c * @param e * @return */ public boolean estMenace(String camp, Case e) { boolean estmenace = false; Vector<Case> listePieceAdverse = new Vector<Case>(); if (camp.equals("noir")) { listePieceAdverse = listerPiecesNoires(this.listePieceEnJeu); } else if (camp.equals("blanc")) { listePieceAdverse = listerPiecesBlanches(this.listePieceEnJeu); } else { System.out.println("case noirblanc"); } for (int i = 0; i < listePieceAdverse.size(); i++) // pour toutes les pieces adverses { ArrayList<Case> deplacementPieceAdverse = filtreGeneral(listePieceAdverse.get(i)); for (int j = 0; j < deplacementPieceAdverse.size(); j++) { // pour tous les deplacements possibles de la piece courante if (deplacementPieceAdverse .get(j) .getPosition() .equals(e.getPosition())) { // Si les deux cases ont la même position estmenace = true; } } } return estmenace; }