01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2006-2007 New York University
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * version 2 as published by the Free Software Foundation.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19:
20: package xtc.xform;
21:
22: import java.util.List;
23: import java.util.Iterator;
24: import java.util.LinkedList;
25:
26: /**
27: * similar external method class.
28: *
29: * @author Joe Pamer
30: * @version $Revision: 1.7 $
31: */
32:
33: class SimilarFunction implements Function {
34:
35: /**
36: * Get the name of the function.
37: *
38: * @return The name of the function.
39: */
40: public String getName() {
41: return "similar";
42: }
43:
44: /**
45: * Test to see if two items are "similar". That is, their objects are
46: * equal, but their parents or indices may not be the same.
47: *
48: * This function takes two arguments. The first is a list of items to
49: * consider for similarity, and the second is a list of items representing
50: * "values" to judge the first list by. Any items in the first list who are
51: * similar to one or more of the items in the second list will be returned.
52: *
53: * @param args The list of two arguments.
54: * @return A non-empty sequence
55: */
56: public Object apply(List<Object> args) {
57: Engine.Sequence<?> arg_a = (Engine.Sequence) args.get(0);
58: Engine.Sequence<?> arg_b = (Engine.Sequence) args.get(1);
59:
60: List<Item> similarList = new LinkedList<Item>();
61:
62: for (Iterator<?> a_iterator = arg_a.flatIterator(); a_iterator
63: .hasNext();) {
64: Item a_item = (Item) a_iterator.next();
65: Object a_object = a_item.object;
66:
67: for (Iterator<?> b_iterator = arg_b.flatIterator(); b_iterator
68: .hasNext();) {
69:
70: if (a_object.equals(((Item) b_iterator.next()).object)) {
71: similarList.add(a_item);
72: }
73: }
74: }
75:
76: return similarList;
77: }
78: } // end class SimilarFunction
|