Skip to content

Commit

Permalink
#363 corrections to encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
elucash committed Aug 18, 2016
1 parent ac8542a commit ce40e38
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 48 deletions.
11 changes: 1 addition & 10 deletions encode/src/org/immutables/encode/Encoding.java
Expand Up @@ -187,16 +187,7 @@ public enum StandardNaming {
PUT("put*", true),

/** builder put all method. */
PUT_ALL("putAll*"),

/** builder is set method. */
IS_SET("*IsSet"),

/** Auxiliary set method. */
SET("set*"),

/** Auxiliary unset method. */
UNSET("unset*");
PUT_ALL("putAll*");

public final String pattern;
public final boolean depluralize;
Expand Down
Expand Up @@ -77,8 +77,8 @@ static <T> List<T> initFromOptional(Optional<List<T>> optional) {
// return elements == null ? null : createSafeList(iterable);
// }

private static <G> List<G> createSafeList(Iterable<G> iterable) {
List<G> list = StreamSupport.stream(iterable.spliterator(), false)
private static <T> List<T> createSafeList(Iterable<T> iterable) {
List<T> list = StreamSupport.stream(iterable.spliterator(), false)
.map(element -> Objects.requireNonNull(element, "element"))
.collect(Collectors.toList());

Expand Down
Expand Up @@ -6,7 +6,7 @@
import org.immutables.fixture.encoding.defs.OptionalList2Enabled;

@Value.Style(depluralize = true)
//@OptionalList2Enabled
@OptionalList2Enabled
@Value.Immutable
public interface UseOptionalCollections2<V> {

Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package org.immutables.value.processor.encode;

import org.immutables.value.processor.encode.Code.Term;
import javax.annotation.Nullable;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package org.immutables.value.processor.encode;

import com.google.common.base.Predicate;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
Expand Down Expand Up @@ -139,12 +140,6 @@ private String generateProperName(EncodedElement element) {
return names.putAll();
case WITH:
return names.with;
case IS_SET:
return names.isSet();
case SET:
return names.set();
case UNSET:
return names.unset();
default:
}
}
Expand Down Expand Up @@ -249,12 +244,13 @@ private void printWithIndentation(Invokation invokation, List<Term> terms) {
for (int i = 0; i < indentLevel + indentWrap; i++) {
invokation.out(" ");
}
// auto-increase indent wrap unless semicolon will return it back
indentWrap = 2;
}

if (t.isDelimiter() && (t.is(';') || t.is('}') || t.is('{'))) {
indentWrap = 0;
} else if (!t.isIgnorable()) {
// auto-increase indent wrap unless semicolon will return it back
indentWrap = 2;
}

// increase indent level after writing a newline
Expand All @@ -274,34 +270,52 @@ private void printWithIndentation(Invokation invokation, List<Term> terms) {
public String apply(EncodedElement input) {
Parameters parameters = Type.Producer.emptyParameters();

// if our method have the same named type parameters as
// encoding, when instantiating it for specific
// attribute, some may resolve to concrete types, some
// may end up value-type specific type parameter
// we need to write only type-specific type parameters omiting those
// which resolves to specific type.
// note that some methods are to be inlined, so this is not needed
// then, it is only needed when non-inlined references are present.

if (input.isFrom()) {
// our from method have the same type parameters as
// encoding in general, when instantiating it for specific
// attribute, some may resolve to concrete types, some
// may end up value-type specific type parameter
// we need to write only type-specific type parameters omiting those
// which resolves to specific type.
// note that often 'from' method is inlined so this is not needed
// then, it is only needed when non-inlined references are present.
// from has implied type parameters, the same as encoding
for (Variable v : typer.variables()) {
Type t = typer.apply(v);
if (t instanceof Type.Variable) {
Type.Variable var = (Type.Variable) t;
parameters = parameters.introduce(var.name, transformBounds(var.upperBounds));
}
parameters = introduceAsEncodingVar(parameters, v);
}
} else {
for (TypeParam p : input.typeParams()) {
parameters = parameters.introduce(p.name(), transformBounds(p.bounds()));
@Nullable Variable encodingVar = typer.byName(p.name());
if (encodingVar != null) {
parameters = introduceAsEncodingVar(parameters, encodingVar);
} else {
parameters = parameters.introduce(p.name(), transformBounds(p.bounds()));
}
}
}

if (parameters.names().isEmpty()) {
return "";
}

return parameters + " ";
}

private Parameters introduceAsEncodingVar(Parameters parameters, Variable encodingVar) {
Type t = typer.apply(encodingVar);
final Parameters[] pHolder = new Parameters[] {parameters};
t.accept(new Type.Transformer() {
@Override
public Type variable(Variable v) {
pHolder[0] = pHolder[0].introduce(v.name, transformBounds(v.upperBounds));
return v;
}
});
parameters = pHolder[0];
return parameters;
}

private ImmutableList<Defined> transformBounds(List<Defined> bounds) {
return FluentIterable.from(bounds)
.transform(typer)
Expand Down
Expand Up @@ -79,11 +79,11 @@ private [inst.typer el.type] with_[inst.namer el]([for p in el.params][if not fo
el in a.instantiation.encoding.element]
[if el.builderMethod]

[declareHelperMethod a.instantiation el]
[declareMethod a.instantiation el]
[else if el.build]
[if not el.oneLiner]

[declareHelperMethod a.instantiation el]
[declareMethod a.instantiation el]
[/if]
[/if]
[/for]
Expand All @@ -94,15 +94,15 @@ private [inst.typer el.type] with_[inst.namer el]([for p in el.params][if not fo
el in a.instantiation.encoding.element]
[if el.valueMethod]

[declareHelperMethod a.instantiation el]
[declareMethod a.instantiation el]
[else if not el.static]
[if el.inlinable and (not el.oneLiner)]

[declareHelperMethod a.instantiation el]
[declareMethod a.instantiation el]
[/if]
[else if el.from]

[declareHelperMethod a.instantiation el]
[declareMethod a.instantiation el]
[/if]
[/for]
[/template]
Expand All @@ -121,11 +121,11 @@ private [inst.typer el.type] with_[inst.namer el]([for p in el.params][if not fo
el in a.instantiation.encoding.element]
[if el.staticMethod]

[declareHelperMethod a.instantiation el]
[else if el.static]
[declareMethod a.instantiation el]
[else if el.static and (not el.from)]
[if el.inlinable and (not el.oneLiner)]

[declareHelperMethod a.instantiation el]
[declareMethod a.instantiation el]
[/if]
[/if]
[/for]
Expand Down Expand Up @@ -153,7 +153,7 @@ private [inst.typer el.type] with_[inst.namer el]([for p in el.params][if not fo
[if el.private]private [else]public [/if][if el.static]static [/if][if el.final]final [/if][inst.typer el.type] [inst.namer el][if el.code] = [inst.codeOf el][/if];
[/template]

[template declareHelperMethod Inst inst Elem el]
[template declareMethod Inst inst Elem el]
[if el.private]private [else]public [/if][if el.static]static [/if][if el.final]final [/if][inst.ownTypeParams el][inst.typer el.type] [inst.namer el]([for p in el.params][if not for.first], [/if][inst.typer p.type] [p.name][/for])[throwsClause inst el] [inst.codeOf el]
[/template]

Expand Down
Expand Up @@ -23,10 +23,7 @@ public enum StandardNaming {
ADD("add*", true),
ADD_ALL("addAll*"),
PUT("put*", true),
PUT_ALL("putAll*"),
IS_SET("*IsSet"),
SET("set*"),
UNSET("unset*");
PUT_ALL("putAll*");

public final String pattern;
public final boolean depluralize;
Expand Down

0 comments on commit ce40e38

Please sign in to comment.