Biblioteca Java - Blame information for rev 18

Subversion Repositories:
Rev:
Rev Author Line No. Line
18 mihai 1 /**
2  * Licensed to Neo Technology under one or more contributor
3  * license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright
5  * ownership. Neo Technology licenses this file to you under
6  * the Apache License, Version 2.0 (the "License"); you may
7  * not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19  
20  
21  
22 package com.linkscreens.graphsin.network;
23  
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.Iterator;
28 import java.util.NoSuchElementException;
29  
30 import org.neo4j.helpers.collection.PositionedIterator;
31  
32 import com.linkscreens.graphsin.repository.PersonNode;
33 import com.linkscreens.graphsin.repository.StatusUpdateNode;
34  
35 public class FriendsStatusUpdateIterator implements Iterator<StatusUpdateNode> {
36     private ArrayList<PositionedIterator<StatusUpdateNode>> statuses = new ArrayList<PositionedIterator<StatusUpdateNode>>();
37     private StatusUpdateComparator comparator = new StatusUpdateComparator();
38  
39     public FriendsStatusUpdateIterator( PersonNode person )
40     {
41         for ( PersonNode friend : person.getFriends() )
42         {
43             Iterator<StatusUpdateNode> iterator = friend.getStatus().iterator();
44             if (iterator.hasNext()) {
45                 statuses.add(new PositionedIterator<StatusUpdateNode>(iterator));
46             }
47         }
48  
49         sort();
50     }
51  
52     public boolean hasNext()
53     {
54         return statuses.size() > 0;
55     }
56  
57     public StatusUpdateNode next()
58     {
59         if ( statuses.size() == 0 )
60         {
61             throw new NoSuchElementException();
62         }
63         // START SNIPPET: getActivityStream
64         PositionedIterator<StatusUpdateNode> first = statuses.get(0);
65         StatusUpdateNode returnVal = first.current();
66  
67         if ( !first.hasNext() )
68         {
69             statuses.remove( 0 );
70         }
71         else
72         {
73             first.next();
74             sort();
75         }
76  
77         return returnVal;
78         // END SNIPPET: getActivityStream
79     }
80  
81     private void sort()
82     {
83         Collections.sort( statuses, comparator );
84     }
85  
86     public void remove()
87     {
88         throw new UnsupportedOperationException( "Don't know how to do that..." );
89     }
90  
91     private class StatusUpdateComparator implements Comparator<PositionedIterator<StatusUpdateNode>> {
92         public int compare(PositionedIterator<StatusUpdateNode> a, PositionedIterator<StatusUpdateNode> b) {
93             return a.current().getDate().compareTo(b.current().getDate());
94         }
95     }
96 }