Skip to content

Commit

Permalink
Half way through messing around with binding/unbinding api
Browse files Browse the repository at this point in the history
thinking about partial binding/unbinding, as well as linked bindings
that are two-way updatable between model and structured data
  • Loading branch information
Sophos-Elias-Vasylenko committed Dec 17, 2015
1 parent 8d0dd53 commit 46b613e
Show file tree
Hide file tree
Showing 21 changed files with 105 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*/
package uk.co.strangeskies.modabi;

import uk.co.strangeskies.modabi.io.structured.NavigableStructuredDataSource;
import uk.co.strangeskies.modabi.io.structured.StructuredDataSource;
import uk.co.strangeskies.modabi.schema.Model;

public interface Binding<T> {
public Model<T> getModel();

public T getData();

public void updateBinding();
public void updateData();

public NavigableStructuredDataSource getSource();
public StructuredDataSource getSource();

public void updateSource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,26 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import uk.co.strangeskies.modabi.io.structured.DiscardingStructuredDataTarget;
import uk.co.strangeskies.modabi.io.structured.RewritableStructuredData;
import uk.co.strangeskies.modabi.io.structured.StructuredDataFormat;
import uk.co.strangeskies.modabi.io.structured.StructuredDataSource;
import uk.co.strangeskies.modabi.io.structured.StructuredDataTarget;
import uk.co.strangeskies.modabi.processing.BindingFuture;
import uk.co.strangeskies.modabi.processing.BindingState;
import uk.co.strangeskies.modabi.processing.UnbindingState;
import uk.co.strangeskies.modabi.schema.BindingNode;
import uk.co.strangeskies.modabi.schema.Model;
import uk.co.strangeskies.reflection.Reified;
import uk.co.strangeskies.reflection.TypeToken;
import uk.co.strangeskies.reflection.TypedObject;

public interface SchemaManager {
interface Binder<T> {
BindingFuture<T> from(StructuredDataSource input);

BindingFuture<T> from(RewritableStructuredData input);

default BindingFuture<T> from(File input) {
return from(input.toURI());
}
Expand All @@ -64,52 +61,13 @@ default BindingFuture<T> from(URI input) {
}
}

default BindingFuture<T> from(URL input) {
String extension = input.getPath();
int lastSlash = extension.lastIndexOf('/');
if (lastSlash > 0) {
extension = extension.substring(lastSlash);

int lastDot = extension.lastIndexOf('.');
if (lastDot > 0) {
extension = extension.substring(lastDot + 1);
} else {
extension = null;
}
} else {
extension = null;
}

try (InputStream fileStream = input.openStream()) {
if (extension != null) {
return from(extension, fileStream);
} else {
return from(fileStream);
}
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
BindingFuture<T> from(URL input);

BindingFuture<T> from(InputStream input);

BindingFuture<T> from(String extension, InputStream input);

default <U> Binder<T> supply(TypeToken<U> type,
Supplier<TypedObject<? extends U>> action) {
return supply(type, s -> action.get());
}

default <U> Binder<T> supply(TypeToken<U> type,
Function<BindingState, TypedObject<? extends U>> action) {
return supply(s -> {
return (s instanceof BindingNode<?, ?, ?> && type.isAssignableFrom(
((BindingNode.Effective<?, ?, ?>) s.bindingNode()).getDataType()))
? action.apply(s) : null;
});
}

Binder<T> supply(Function<BindingState, TypedObject<?>> action);
Binder<T> updatable();

/*
* Errors which are rethrown will be passed to the next error handler if
Expand All @@ -120,7 +78,9 @@ default <U> Binder<T> supply(TypeToken<U> type,
}

interface Unbinder<T> {
BindingFuture<T> to(StructuredDataTarget output);
<U extends StructuredDataTarget> U to(U output);

BindingFuture<T> to(RewritableStructuredData output);

default BindingFuture<T> to(File output) {
return to(output.toURI());
Expand Down Expand Up @@ -156,15 +116,7 @@ default BindingFuture<T> to(URL output) {

BindingFuture<T> to(String extension, OutputStream output);

Unbinder<T> consume(Predicate<UnbindingState> filter);

default <U> Unbinder<T> consume(TypeToken<U> type,
Predicate<TypedObject<? extends U>> filter) {
return consume(type, (s, o) -> filter.test(o));
}

<U> Unbinder<T> consume(TypeToken<U> type,
BiPredicate<UnbindingState, TypedObject<? extends U>> filter);
Unbinder<T> updatable();

/*
* Errors which are rethrown will be passed to the next error handler if
Expand Down Expand Up @@ -208,7 +160,10 @@ default <T> void registerProvider(Class<T> providedClass,
boolean registerSchema(Schema schema);

default <T> BindingFuture<T> registerBinding(Model<T> model, T data) {
return unbind(model, data).to(new DiscardingStructuredDataTarget());
BindingFuture<T> binding = unbind(model, data)
.to(new DiscardingStructuredDataTarget());
binding.resolve();
return binding;
}

// Blocks until all possible processing is done other than waiting imports:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public QualifiedName startNextChild() {
if (buffer.index().equals(component.index())) {
QualifiedName child = component.startNextChild();
if (child != null) {
buffers.nextChild(child);
buffers.addChild(child);
component.pipeDataAtChild(buffers);
}
}
Expand Down Expand Up @@ -135,13 +135,15 @@ public List<String> getComments() {
}

@Override
public void endChild() {
public StructuredDataSource endChild() {
if (buffer.index().equals(component.index())) {
component.endChild();
buffers.endChild();
}

buffer.endChild();

return this;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.co.strangeskies.modabi.io.structured;

public interface RewritableStructuredData
extends NavigableStructuredDataSource, StructuredDataTarget {
@Override
RewritableStructuredData endChild();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public QualifiedName startNextChild() {
}

@Override
public void endChild() {
public StructuredDataSource endChild() {
if (!peekTail().isEnded())
throw new IllegalStateException();

Expand All @@ -469,6 +469,8 @@ public void endChild() {
if (consumable) {
peekTail().children.remove(0);
}

return this;
}

private int getActualTailIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public interface StructuredDataFormat {
StructuredDataSource loadData(InputStream in);

StructuredDataTarget saveData(OutputStream out);

RewritableStructuredData modifyData(/*
* Some sort of generic rewritable &
* navigable byte data interface...
*/);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public interface StructuredDataSource {

public QualifiedName peekNextChild();

public default void startNextChild(QualifiedName name) {
QualifiedName nextName = startNextChild();
public default StructuredDataSource startNextChild(QualifiedName name) {
QualifiedName nextName = peekNextChild();
if (!nextName.equals(name)) {
throw new SchemaException("Next child '" + nextName
+ "' does not match expected name '" + name + "'");
}
startNextChild();
return this;
}

public Set<QualifiedName> getProperties();
Expand All @@ -65,21 +67,17 @@ public default boolean skipNextChild() {
return hasNext;
}

public default void skipChildren() {
public default StructuredDataSource skipChildren() {
while (skipNextChild())
;
return this;
}

/**
* throws an exception if there are more children, so call skipChildren()
* first, or call endChildEarly, if you want to ignore them.
*/
public void endChild();

public default void endChildEarly() {
skipChildren();
endChild();
}
public StructuredDataSource endChild();

public List<Integer> index();

Expand All @@ -96,7 +94,7 @@ public default <T extends StructuredDataTarget> T pipeNextChild(T output) {
do {
while (output.currentState() != StructuredDataState.ELEMENT_WITH_CONTENT
&& (childElement = startNextChild()) != null) {
output.nextChild(childElement);
output.addChild(childElement);

pipeDataAtChild(output);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ public DataSource readContent() {
}

@Override
public void endChild() {
public StructuredDataSource endChild() {
if (index().isEmpty())
enterState(StructuredDataState.FINISHED);
else
enterState(StructuredDataState.POPULATED_ELEMENT);
getComponent().endChild();

return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface StructuredDataTarget {

public StructuredDataTarget registerNamespaceHint(Namespace namespace);

public StructuredDataTarget nextChild(QualifiedName name);
public StructuredDataTarget addChild(QualifiedName name);

public DataTarget writeProperty(QualifiedName name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public S comment(String comment) {
protected abstract void commentImpl(String comment);

@Override
public S nextChild(QualifiedName name) {
public S addChild(QualifiedName name) {
index.push(0);
enterState(StructuredDataState.ELEMENT_START);
nextChildImpl(name);
Expand Down
Loading

0 comments on commit 46b613e

Please sign in to comment.