Skip to content

Commit 13545ab

Browse files
BradBot1dmulloy2
authored andcommitted
Add StructureModifierIntermediate for r/w access
1 parent 71c39a2 commit 13545ab

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* @param <T> Type of the fields to retrieve.
5454
* @author Kristian
5555
*/
56-
public class StructureModifier<T> implements Iterable<T> {
56+
public class StructureModifier<T> implements Iterable<StructureModifierIntermediate<T>> {
5757

5858
// Instance generator we will use
5959
private static final DefaultInstances DEFAULT_GENERATOR = getDefaultGenerator();
@@ -673,7 +673,7 @@ public String toString() {
673673
* {@inheritDoc}
674674
*/
675675
@Override
676-
public Iterator<T> iterator() {
676+
public Iterator<StructureModifierIntermediate<T>> iterator() {
677677
return new StructureModifierIterator<T>(this);
678678
}
679679
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
/**
20+
* Created by {@link StructureModifierIterator} iterator access to a {@link StructureModifier}
21+
*
22+
* @param <T> Type of the fields in the {@link StructureModifier}
23+
* @author BradBot_1
24+
*/
25+
public class StructureModifierIntermediate<T> {
26+
27+
protected final StructureModifier<T> structure;
28+
protected final int index;
29+
30+
/**
31+
* @param structure - {@link StructureModifier} to operate on.
32+
* @param index - index to access.
33+
*
34+
* @apiNote Must be public, else extending it would not work properly.
35+
*/
36+
public StructureModifierIntermediate(final StructureModifier<T> structure, final int index) {
37+
this.structure = structure;
38+
this.index = index;
39+
}
40+
41+
/**
42+
* @return Object at index.
43+
*/
44+
public T get() {
45+
return this.structure.read(this.index);
46+
}
47+
48+
/**
49+
* Overwrites the existing value at the index in the structure.
50+
*
51+
* @param toWrite - Object to overwrite the existing one.
52+
*/
53+
public void set(final T toWrite) {
54+
this.structure.write(index, toWrite);
55+
}
56+
57+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @param <T> Type of the fields in the {@link StructureModifier}
2525
* @author BradBot_1
2626
*/
27-
class StructureModifierIterator<T> implements Iterator<T> {
27+
class StructureModifierIterator<T> implements Iterator<StructureModifierIntermediate<T>> {
2828

2929
private final StructureModifier<T> structure;
3030
private int index = 0;
@@ -40,17 +40,17 @@ class StructureModifierIterator<T> implements Iterator<T> {
4040
* {@inheritDoc}
4141
*/
4242
@Override
43-
public final boolean hasNext() {
43+
public boolean hasNext() {
4444
return this.index < this.structure.size();
4545
}
4646

4747
/**
4848
* {@inheritDoc}
4949
*/
5050
@Override
51-
public final T next() {
51+
public StructureModifierIntermediate<T> next() {
5252
if (!this.hasNext()) return null;
53-
return structure.read(index++);
53+
return new StructureModifierIntermediate<T>(this.structure, this.index++);
5454
}
5555

5656
}

0 commit comments

Comments
 (0)