/[fenfire]/fenfire/org/fenfire/loom/Cursor.java
ViewVC logotype

Diff of /fenfire/org/fenfire/loom/Cursor.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by benja, Fri Feb 28 17:41:28 2003 UTC revision 1.2 by benja, Tue Mar 4 10:50:43 2003 UTC
# Line 50  public class Cursor { Line 50  public class Cursor {
50       */       */
51      public Comparator order;      public Comparator order;
52    
53        /** Compare statements by their subjects.
54         *  <code>subjOrder</code> orders statements by applying
55         *  <code>order</code> to their subjects.
56         */
57        protected Comparator subjOrder = new Comparator() {
58                public int compare(Object o1, Object o2) {
59                    Statement s1 = (Statement)o1, s2 = (Statement)o2;
60                    return order.compare(s1.getSubject(), s2.getSubject());
61                }
62            };
63    
64        /** Compare statements by their objects.
65         *  <code>objOrder</code> orders statements by applying
66         *  <code>order</code> to their objects.
67         */
68        protected Comparator objOrder = new Comparator() {
69                public int compare(Object o1, Object o2) {
70                    Statement s1 = (Statement)o1, s2 = (Statement)o2;
71                    return order.compare(s1.getObject(), s2.getObject());
72                }
73            };
74    
75      /** Create a new cursor without giving it a position yet.      /** Create a new cursor without giving it a position yet.
76       */       */
77      public Cursor(Comparator order) {      public Cursor(Comparator order) {
# Line 72  public class Cursor { Line 94  public class Cursor {
94      }      }
95    
96      /** Set the position of the cursor.      /** Set the position of the cursor.
      *  Ambiguity of <code>rotation</code> (is it posward  
      *  or negward?) might be a problem?  
97       */       */
98      public void set(Resource focus, int dir, RDFNode rotation) {      public void set(Resource focus, int dir, RDFNode rotation) {
99          this.focus = focus;          this.focus = focus;
100          SortedSet set = getConnections(dir);          SortedSet set = getConnections(dir);
101          if(!set.contains(rotation))          Iterator iter=set.iterator();
102              throw new NoSuchElementException("Rotation: "+rotation);          for(int i=0; iter.hasNext(); i++) {
103                Statement stmt = (Statement)iter.next();
104                RDFNode node = (dir<0) ? stmt.getSubject() : stmt.getObject();
105                if(node.equals(rotation)) {
106                    this.rotation = i-(set.size()-1)/2;
107                    return;
108                }
109            }
110    
111          this.rotation = set.headSet(rotation).size()-(set.size()-1)/2;          throw new NoSuchElementException("Rotation: "+rotation);
112      }      }
113    
114      /** Get <code>rotation</code> from the middle connection.      /** Get <code>rotation</code> from the middle connection.
# Line 96  public class Cursor { Line 123  public class Cursor {
123       *  in order. XXX refactor-- we need only a subset of all properties,       *  in order. XXX refactor-- we need only a subset of all properties,
124       *  and recursion... currently slow because we iterate though       *  and recursion... currently slow because we iterate though
125       *  ALL triples in the graph.       *  ALL triples in the graph.
126         *  <p>
127         *  Returns a set of <em>statements</em>, not a set of nodes.
128       */       */
129      public SortedSet getConnections(int dir) {      public SortedSet getConnections(int dir) {
130          try {          try {
131              SortedSet nodes = new TreeSet(order);              SortedSet nodes = new TreeSet(dir<0 ? subjOrder : objOrder);
132              StmtIterator i = focus.getModel().listStatements();              StmtIterator i = focus.getModel().listStatements();
133              for(; i.hasNext();) {              for(; i.hasNext();) {
134                  Statement stmt = i.next();                  Statement stmt = i.next();
135                  if(dir < 0) {                  if(dir < 0) {
136                      if(stmt.getObject().equals(focus))                      if(stmt.getObject().equals(focus))
137                          nodes.add(stmt.getSubject());                          nodes.add(stmt);
138                  } else {                  } else {
139                      if(stmt.getSubject().equals(focus))                      if(stmt.getSubject().equals(focus))
140                          nodes.add(stmt.getObject());                          nodes.add(stmt);
141                  }                  }
142              }              }
143              i.close();              i.close();
# Line 173  public class Cursor { Line 202  public class Cursor {
202              // nothing there              // nothing there
203              return null;              return null;
204    
205          return (RDFNode)iter.next();          Statement stmt = (Statement)iter.next();
206            return (dir<0) ? stmt.getSubject() : stmt.getObject();
207      }      }
208    
209      /** Move left/right. XXX      /** Move left/right. XXX

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26