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 org.neo4j.examples.socnet;
23  
24  import org.neo4j.helpers.collection.PositionedIterator;
25  
26  import java.util.*;
27  
28  class FriendsStatusUpdateIterator implements Iterator<StatusUpdate> {
29      private ArrayList<PositionedIterator<StatusUpdate>> statuses = new ArrayList<PositionedIterator<StatusUpdate>>();
30      private StatusUpdateComparator comparator = new StatusUpdateComparator();
31  
32      public FriendsStatusUpdateIterator( Person person )
33      {
34          for ( Person friend : person.getFriends() )
35          {
36              Iterator<StatusUpdate> iterator = friend.getStatus().iterator();
37              if (iterator.hasNext()) {
38                  statuses.add(new PositionedIterator<StatusUpdate>(iterator));
39              }
40          }
41  
42          sort();
43      }
44  
45      public boolean hasNext()
46      {
47          return statuses.size() > 0;
48      }
49  
50      public StatusUpdate next()
51      {
52          if ( statuses.size() == 0 )
53          {
54              throw new NoSuchElementException();
55          }
56          // START SNIPPET: getActivityStream
57          PositionedIterator<StatusUpdate> first = statuses.get(0);
58          StatusUpdate returnVal = first.current();
59  
60          if ( !first.hasNext() )
61          {
62              statuses.remove( 0 );
63          }
64          else
65          {
66              first.next();
67              sort();
68          }
69  
70          return returnVal;
71          // END SNIPPET: getActivityStream
72      }
73  
74      private void sort()
75      {
76          Collections.sort( statuses, comparator );
77      }
78  
79      public void remove()
80      {
81          throw new UnsupportedOperationException( "Don't know how to do that..." );
82      }
83  
84      private class StatusUpdateComparator implements Comparator<PositionedIterator<StatusUpdate>> {
85          public int compare(PositionedIterator<StatusUpdate> a, PositionedIterator<StatusUpdate> b) {
86              return a.current().getDate().compareTo(b.current().getDate());
87          }
88      }
89  }