001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.repository; 017 018import java.util.Collection; 019import java.util.Iterator; 020 021/** 022 * Basic implementation of an {@link AmetysObjectIterable} based on an existing {@link Collection}. 023 * @param <A> the actual type of {@link AmetysObject}s 024 */ 025public class CollectionIterable<A extends AmetysObject> implements AmetysObjectIterable<A> 026{ 027 private Collection<A> _collection; 028 029 /** 030 * Creates a {@link CollectionIterable}. 031 * @param collection the {@link AmetysObject}s collection 032 */ 033 public CollectionIterable(Collection<A> collection) 034 { 035 _collection = collection; 036 } 037 038 public long getSize() 039 { 040 return _collection.size(); 041 } 042 043 public AmetysObjectIterator<A> iterator() 044 { 045 return new CollectionIterator(_collection.iterator(), _collection.size()); 046 } 047 048 public void close() 049 { 050 // nothing to do 051 } 052 053 class CollectionIterator implements AmetysObjectIterator<A> 054 { 055 private Iterator<A> _it; 056 private int _pos; 057 private long _size; 058 059 public CollectionIterator(Iterator<A> it, long size) 060 { 061 _it = it; 062 _size = size; 063 } 064 065 public boolean hasNext() 066 { 067 return _it.hasNext(); 068 } 069 070 public A next() 071 { 072 _pos++; 073 return _it.next(); 074 } 075 076 public long getSize() 077 { 078 return _size; 079 } 080 081 public long getPosition() 082 { 083 return _pos; 084 } 085 } 086}