Skip to content

Commit 7be5517

Browse files
committed
Make StructureModifier Iterable
1 parent 81b1644 commit 7be5517

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/main/java/com/comphenix/protocol/reflect/StructureModifier.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.google.common.collect.HashBasedTable;
2727
import com.google.common.collect.Table;
2828

29+
import java.lang.Iterable;
30+
import java.util.Iterator;
2931
import java.lang.ref.Reference;
3032
import java.lang.ref.SoftReference;
3133
import java.lang.reflect.Field;
@@ -51,7 +53,7 @@
5153
* @param <T> Type of the fields to retrieve.
5254
* @author Kristian
5355
*/
54-
public class StructureModifier<T> {
56+
public class StructureModifier<T> implements Iterable<T> {
5557

5658
// Instance generator we will use
5759
private static final DefaultInstances DEFAULT_GENERATOR = getDefaultGenerator();
@@ -666,4 +668,12 @@ protected void setConverter(EquivalentConverter<T> converter) {
666668
public String toString() {
667669
return "StructureModifier[fieldType=" + this.fieldType + ", data=" + this.accessors + "]";
668670
}
671+
672+
/**
673+
* {@inheritDoc}
674+
*/
675+
@Override
676+
public Iterator<T> iterator() {
677+
return new StructureModifierIterator<T>(this);
678+
}
669679
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
3+
* Copyright (C) 2012 Kristian S. Stangeland
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation; either version 2 of
7+
* the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
* See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with this program;
14+
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
15+
* 02111-1307 USA
16+
*/
17+
package com.comphenix.protocol.reflect;
18+
19+
import java.util.Iterator;
20+
21+
/**
22+
* Provides iterator access to a {@link StructureModifier}
23+
*
24+
* @param <T> Type of the fields in the {@link StructureModifier}
25+
* @author BradBot_1
26+
*/
27+
class StructureModifierIterator<T> implements Iterator<T> {
28+
29+
private final StructureModifier<T> structure;
30+
private int index = 0;
31+
32+
/**
33+
* @param structure - {@link StructureModifier} to operate on.
34+
*/
35+
StructureModifierIterator(final StructureModifier<T> structure) {
36+
this.structure = structure;
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
@Override
43+
public final boolean hasNext() {
44+
return this.index < this.structure.size();
45+
}
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
50+
@Override
51+
public final T next() {
52+
if (!this.hasNext()) return null;
53+
return structure.read(index++);
54+
}
55+
56+
}

0 commit comments

Comments
 (0)