Biblioteca Java - Rev 18

Subversion Repositories:
Rev:
/**
 * Licensed to Neo Technology under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Neo Technology licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */




package com.linkscreens.graphsin.network;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.neo4j.helpers.collection.PositionedIterator;

import com.linkscreens.graphsin.repository.PersonNode;
import com.linkscreens.graphsin.repository.StatusUpdateNode;

public class FriendsStatusUpdateIterator implements Iterator<StatusUpdateNode> {
    private ArrayList<PositionedIterator<StatusUpdateNode>> statuses = new ArrayList<PositionedIterator<StatusUpdateNode>>();
    private StatusUpdateComparator comparator = new StatusUpdateComparator();

    public FriendsStatusUpdateIterator( PersonNode person )
    {
        for ( PersonNode friend : person.getFriends() )
        {
            Iterator<StatusUpdateNode> iterator = friend.getStatus().iterator();
            if (iterator.hasNext()) {
                statuses.add(new PositionedIterator<StatusUpdateNode>(iterator));
            }
        }

        sort();
    }

    public boolean hasNext()
    {
        return statuses.size() > 0;
    }

    public StatusUpdateNode next()
    {
        if ( statuses.size() == 0 )
        {
            throw new NoSuchElementException();
        }
        // START SNIPPET: getActivityStream
        PositionedIterator<StatusUpdateNode> first = statuses.get(0);
        StatusUpdateNode returnVal = first.current();

        if ( !first.hasNext() )
        {
            statuses.remove( 0 );
        }
        else
        {
            first.next();
            sort();
        }

        return returnVal;
        // END SNIPPET: getActivityStream
    }

    private void sort()
    {
        Collections.sort( statuses, comparator );
    }

    public void remove()
    {
        throw new UnsupportedOperationException( "Don't know how to do that..." );
    }

    private class StatusUpdateComparator implements Comparator<PositionedIterator<StatusUpdateNode>> {
        public int compare(PositionedIterator<StatusUpdateNode> a, PositionedIterator<StatusUpdateNode> b) {
            return a.current().getDate().compareTo(b.current().getDate());
        }
    }
}