Skip to content

Commit

Permalink
Start work on #433
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 29, 2017
1 parent 4694a80 commit 7403cdd
Show file tree
Hide file tree
Showing 93 changed files with 430 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.json.JsonFactory;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.core.type.WritableTypeId.Inclusion;
import com.fasterxml.jackson.core.util.VersionUtil;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/fasterxml/jackson/core/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.math.BigInteger;

import com.fasterxml.jackson.core.async.NonBlockingInputFeeder;
import com.fasterxml.jackson.core.json.JsonFactory;
import com.fasterxml.jackson.core.sym.FieldNameMatcher;
import com.fasterxml.jackson.core.type.ResolvedType;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -524,7 +525,7 @@ public NonBlockingInputFeeder getNonBlockingInputFeeder() {
* {@link Feature#AUTO_CLOSE_SOURCE} is enabled.
* Whether parser owns the input source depends on factory
* method that was used to construct instance (so check
* {@link com.fasterxml.jackson.core.JsonFactory} for details,
* {@link com.fasterxml.jackson.core.json.JsonFactory} for details,
* but the general
* idea is that if caller passes in closable resource (such
* as {@link InputStream} or {@link Reader}) parser does NOT
Expand Down
185 changes: 185 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/TokenStreamFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;

import com.fasterxml.jackson.core.io.*;
import com.fasterxml.jackson.core.json.JsonFactory;
import com.fasterxml.jackson.core.sym.FieldNameMatcher;
import com.fasterxml.jackson.core.sym.SimpleNameMatcher;
import com.fasterxml.jackson.core.util.BufferRecycler;
Expand Down Expand Up @@ -137,6 +138,139 @@ public static int collectDefaults() {
public int getMask() { return (1 << ordinal()); }
}

/**
* Since factory instances are immutable, a Builder class is needed for creating
* configurations for differently configured factory instances.
*
* @since 3.0
*/
public abstract static class TSFBuilder<F extends TokenStreamFactory,
T extends TSFBuilder<F,T>>
{
/**
* Set of {@link TokenStreamFactory.Feature}s enabled, as bitmask.
*/
protected int _factoryFeatures;

/**
* Set of {@link JsonParser.Feature}s enabled, as bitmask.
*/
protected int _parserFeatures;

/**
* Set of {@link JsonGenerator.Feature}s enabled, as bitmask.
*/
protected int _generatorFeatures;

// // // Construction

protected TSFBuilder() {
_factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
_parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
_generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
}

protected TSFBuilder(TokenStreamFactory base)
{
this(base._factoryFeatures,
base.getParserFeatures(), base.getGeneratorFeatures());
}

protected TSFBuilder(int factoryFeatures,
int parserFeatures, int generatorFeatures)
{
_factoryFeatures = factoryFeatures;
_parserFeatures = parserFeatures;
_generatorFeatures = generatorFeatures;
}

// // // Accessors

public int factoryFeaturesMask() { return _factoryFeatures; }
public int parserFeaturesMask() { return _parserFeatures; }
public int generatorFeaturesMask() { return _generatorFeatures; }

// // // Factory features

public T with(TokenStreamFactory.Feature f) {
_factoryFeatures |= f.getMask();
return _this();
}

public T without(TokenStreamFactory.Feature f) {
_factoryFeatures &= ~f.getMask();
return _this();
}

// // // Parser features

public T with(JsonParser.Feature f) {
_parserFeatures |= f.getMask();
return _this();
}

public T with(JsonParser.Feature first, JsonParser.Feature... other) {
_parserFeatures |= first.getMask();
for (JsonParser.Feature f : other) {
_parserFeatures |= f.getMask();
}
return _this();
}

public T without(JsonParser.Feature f) {
_parserFeatures &= ~f.getMask();
return _this();
}

public T without(JsonParser.Feature first, JsonParser.Feature... other) {
_parserFeatures &= ~first.getMask();
for (JsonParser.Feature f : other) {
_parserFeatures &= ~f.getMask();
}
return _this();
}

// // // Generator features

public T with(JsonGenerator.Feature f) {
_generatorFeatures |= f.getMask();
return _this();
}

public T with(JsonGenerator.Feature first, JsonGenerator.Feature... other) {
_generatorFeatures |= first.getMask();
for (JsonGenerator.Feature f : other) {
_parserFeatures |= f.getMask();
}
return _this();
}

public T without(JsonGenerator.Feature f) {
_generatorFeatures &= ~f.getMask();
return _this();
}

public T without(JsonGenerator.Feature first, JsonGenerator.Feature... other) {
_generatorFeatures &= ~first.getMask();
for (JsonGenerator.Feature f : other) {
_generatorFeatures &= ~f.getMask();
}
return _this();
}

// // // Other methods

/**
* Method for constructing actual {@link TokenStreamFactory} instance, given
* configuration.
*/
protected abstract F build();

// silly convenience cast method we need
@SuppressWarnings("unchecked")
protected final T _this() { return (T) this; }
}

/*
/**********************************************************
/* Constants
Expand Down Expand Up @@ -199,6 +333,22 @@ public static int collectDefaults() {
*/
public TokenStreamFactory() { }

/**
* Constructors used by {@link TSFBuilder} for instantiation. Base builder is
* passed as-is to try to make interface between base types and implementations
* less likely to change (given that sub-classing is a fragile way to do it):
* if and when new general-purpose properties are added, implementation classes
* do not have to use different constructors.
*
* @since 3.0
*/
protected TokenStreamFactory(TSFBuilder<?,?> baseBuilder)
{
_factoryFeatures = baseBuilder.factoryFeaturesMask();
_parserFeatures = baseBuilder.parserFeaturesMask();
_generatorFeatures = baseBuilder.generatorFeaturesMask();
}

/**
* Constructor used when copy()ing a factory instance.
*/
Expand All @@ -217,6 +367,14 @@ protected TokenStreamFactory(TokenStreamFactory src)
*/
public abstract TokenStreamFactory copy();

/**
* Method that can be used to create differently configured stream factories.
*
* @since 3.0
*/
public abstract TSFBuilder<?,?> rebuild();
// public abstract <F extends TokenStreamFactory, T extends TSFBuilder<F,T>> TSFBuilder<F,T> rebuild();

/*
/**********************************************************
/* Capability introspection
Expand Down Expand Up @@ -600,46 +758,73 @@ public abstract JsonParser createParser(ObjectReadContext readCtxt,
/**********************************************************
*/

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,java.io.File)}
*/
@Deprecated
public JsonParser createParser(File src) throws IOException {
return createParser(ObjectReadContext.empty(), src);
}

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,java.net.URL)}
*/
@Deprecated
public JsonParser createParser(URL src) throws IOException {
return createParser(ObjectReadContext.empty(), src);
}

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,java.io.InputStream)}
*/
@Deprecated
public JsonParser createParser(InputStream in) throws IOException {
return createParser(ObjectReadContext.empty(), in);
}

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,java.io.Reader)}
*/
@Deprecated
public JsonParser createParser(Reader r) throws IOException {
return createParser(ObjectReadContext.empty(), r);
}

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,byte[])}
*/
@Deprecated
public JsonParser createParser(byte[] data) throws IOException {
return createParser(ObjectReadContext.empty(), data, 0, data.length);
}

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,byte[],int,int)}
*/
@Deprecated
public JsonParser createParser(byte[] data, int offset, int len) throws IOException {
return createParser(ObjectReadContext.empty(), data, offset, len);
}

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,String)}
*/
@Deprecated
public JsonParser createParser(String content) throws IOException {
return createParser(ObjectReadContext.empty(), content);
}

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,char[])}
*/
@Deprecated
public JsonParser createParser(char[] content) throws IOException {
return createParser(ObjectReadContext.empty(), content, 0, content.length);
}

/**
* @deprecated Since 3.0 use {@link #createParser(ObjectReadContext,char[],int,int)}
*/
@Deprecated
public JsonParser createParser(char[] content, int offset, int len) throws IOException {
return createParser(ObjectReadContext.empty(), content, offset, len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Package that contains abstractions needed to support optional
* non-blocking decoding (parsing) functionality.
* Although parsers are constructed normally via
* {@link com.fasterxml.jackson.core.JsonFactory}
* {@link com.fasterxml.jackson.core.json.JsonFactory}
* (and are, in fact, sub-types of {@link com.fasterxml.jackson.core.JsonParser}),
* the way input is provided differs.
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/base/BinaryTSFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public abstract class BinaryTSFactory extends DecorableTSFactory

protected BinaryTSFactory() { super(); }

/**
* Constructors used by builders for instantiation.
*
* @since 3.0
*/
protected BinaryTSFactory(DecorableTSFBuilder<?,?> baseBuilder)
{
super(baseBuilder);
}

/**
* Constructor used when copy()ing a factory instance.
*/
Expand Down
Loading

0 comments on commit 7403cdd

Please sign in to comment.