Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make StructureModifier Iterable #2477

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

import java.lang.Iterable;
import java.util.Iterator;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
Expand All @@ -40,8 +42,11 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Provides list-oriented access to the fields of a Minecraft packet.
Expand All @@ -51,7 +56,7 @@
* @param <T> Type of the fields to retrieve.
* @author Kristian
*/
public class StructureModifier<T> {
public class StructureModifier<T> implements Iterable<StructureModifierIntermediate<T>> {

// Instance generator we will use
private static final DefaultInstances DEFAULT_GENERATOR = getDefaultGenerator();
Expand Down Expand Up @@ -666,4 +671,20 @@ protected void setConverter(EquivalentConverter<T> converter) {
public String toString() {
return "StructureModifier[fieldType=" + this.fieldType + ", data=" + this.accessors + "]";
}

/**
* {@inheritDoc}
*/
@Override
public Iterator<StructureModifierIntermediate<T>> iterator() {
return new StructureModifierIterator<T>(this);
}

/**
* {@inheritDoc}
*/
@Override
public Spliterator<StructureModifierIntermediate<T>> spliterator() {
return Spliterators.spliterator(this.iterator(), this.size(), Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.ORDERED | Spliterator.SIZED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2012 Kristian S. Stangeland
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
package com.comphenix.protocol.reflect;

/**
* Created by {@link StructureModifierIterator} iterator access to a {@link StructureModifier}
*
* @param <T> Type of the fields in the {@link StructureModifier}
* @author BradBot_1
*/
public class StructureModifierIntermediate<T> {

protected final StructureModifier<T> structure;
protected final int index;

/**
* @param structure - {@link StructureModifier} to operate on.
* @param index - index to access.
*
* @apiNote Must be public, else extending it would not work properly.
*/
public StructureModifierIntermediate(final StructureModifier<T> structure, final int index) {
this.structure = structure;
this.index = index;
}

/**
* @return Object at index.
*/
public T get() {
return this.structure.read(this.index);
}

/**
* Overwrites the existing value at the index in the structure.
*
* @param toWrite - Object to overwrite the existing one.
*/
public void set(final T toWrite) {
this.structure.write(index, toWrite);
}

/**
* @return The internal structure modifier
*/
public StructureModifier<T> getInternalStructureModifier() {
return this.structure;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2012 Kristian S. Stangeland
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
package com.comphenix.protocol.reflect;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* Provides iterator access to a {@link StructureModifier}
*
* @param <T> Type of the fields in the {@link StructureModifier}
* @author BradBot_1
*/
class StructureModifierIterator<T> implements Iterator<StructureModifierIntermediate<T>> {

private final StructureModifier<T> structure;
private int index = 0;

/**
* @param structure - {@link StructureModifier} to operate on.
*/
StructureModifierIterator(final StructureModifier<T> structure) {
this.structure = structure;
}

/**
* {@inheritDoc}
*/
@Override
public boolean hasNext() {
return this.index < this.structure.size();
}

/**
* {@inheritDoc}
*/
@Override
public StructureModifierIntermediate<T> next() {
if (!this.hasNext()) throw new NoSuchElementException();
return new StructureModifierIntermediate<T>(this.structure, this.index++);
}

}
Loading