
br.com.objectos.html.io.ValueList Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2016 Objectos, Fábrica de Software LTDA.
*
* Licensed 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 br.com.objectos.html.io;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author [email protected] (Marcio Endo)
*/
abstract class ValueList {
private static final ValueList EMPTY = new Empty();
private ValueList() {
}
public static ValueList empty() {
return EMPTY;
}
public abstract ValueList add(SingleValue value);
public abstract void consume(HtmlReader reader);
private static class Empty extends ValueList {
@Override
public ValueList add(SingleValue value) {
return new One(value);
}
@Override
public void consume(HtmlReader reader) {
// noop
}
}
private static class One extends ValueList {
private final Value> value;
public One(Value> value) {
this.value = value;
}
@Override
public ValueList add(SingleValue value) {
return new Many(this.value, value);
}
@Override
public void consume(HtmlReader reader) {
value.consume(reader);
}
}
private static class Many extends ValueList {
private final List> list = new ArrayList<>(4);
public Many(Value> first, SingleValue second) {
list.add(first);
list.add(second);
}
@Override
public ValueList add(SingleValue value) {
list.add(value);
return this;
}
@Override
public void consume(HtmlReader reader) {
List> remaining = new ArrayList<>(list);
List> temp = new ArrayList<>(remaining.size());
Iterator iterator = reader.iterator();
while (iterator.hasNext()) {
TagPojo tag = iterator.next();
for (Value> value : remaining) {
value.consumeTag(tag);
value.removeNextIteration(temp);
}
remaining.removeAll(temp);
temp.clear();
if (remaining.isEmpty()) {
break;
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy