Skip to content

Commit

Permalink
Replace PrinterOption with ProblemPrinterBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
leadpony committed Mar 15, 2019
1 parent c580b3c commit 6e3842d
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,46 +375,34 @@ JsonReaderFactory createReaderFactory(Map<String, ?> config, JsonSchema schema,
/**
* Creates a problem handler which will print problems with the aid of the
* specified line consumer.
*
* <p>
* The problem printer will be created with the default configuration including
* both {@link PrinterOption#INCLUDE_LOCATION} and
* {@link PrinterOption#INCLUDE_POINTER}.
* If a customized printer is needed,
* {@link #createProblemPrinterBuilder(Consumer)} should be used instead of this
* method.
* </p>
*
* @param lineConsumer the object which will output the line to somewhere.
* @param lineConsumer the object which will output the message lines to
* somewhere.
* @return newly created instance of problem handler.
* @throws NullPointerException if the specified {@code lineConsumer} is
* {@code null}.
*/
default ProblemHandler createProblemPrinter(Consumer<String> lineConsumer) {
return createProblemPrinter(lineConsumer, Locale.getDefault());
}

/**
* Creates a problem handler which will print problems with the aid of the
* specified line consumer. One or more options can be specified.
*
* @param lineConsumer the object which will output the line to somewhere.
* @param options one or more options for the problem printer.
* @return newly created instance of problem handler.
* @throws NullPointerException if the specified {@code lineConsumer} or
* {@code options} is {@code null}.
*/
default ProblemHandler createProblemPrinter(Consumer<String> lineConsumer, PrinterOption... options) {
return createProblemPrinter(lineConsumer, Locale.getDefault(), options);
}
ProblemHandler createProblemPrinter(Consumer<String> lineConsumer);

/**
* Creates a problem handler which will print problems with the aid of the
* specified line consumer. The messages will be localized for the specified
* locale.
*
* <p>
* The problem printer will be created with the default configuration including
* both {@link PrinterOption#INCLUDE_LOCATION} and
* {@link PrinterOption#INCLUDE_POINTER}.
* If a customized printer is needed,
* {@link #createProblemPrinterBuilder(Consumer)} should be used instead of this
* method.
* </p>
*
* @param lineConsumer the object which will output the line to somewhere.
* @param lineConsumer the object which will output the message lines to
* somewhere.
* @param locale the locale for which the problem messages will be
* localized.
* @return newly created instance of problem handler.
Expand All @@ -424,17 +412,15 @@ default ProblemHandler createProblemPrinter(Consumer<String> lineConsumer, Print
ProblemHandler createProblemPrinter(Consumer<String> lineConsumer, Locale locale);

/**
* Creates a problem handler which will print problems with the aid of the
* specified line consumer. The messages will be localized for the specified
* locale. One or more options can be specified.
* Creates a builder instance which can be used to build a customized problem
* printer.
*
* @param lineConsumer the object which will output the line to somewhere.
* @param locale the locale for which the problem messages will be
* localized.
* @param options one or more options for the problem printer.
* @return newly created instance of problem handler.
* @throws NullPointerException if the one of the specified parameters is
* @param lineConsumer the object which will output the message lines to
* somewhere.
* @return newly created instance of problem printer builder, which can be used
* to build a problem printer.
* @throws NullPointerException if the specified {@code lineConsumer} is
* {@code null}.
*/
ProblemHandler createProblemPrinter(Consumer<String> lineConsumer, Locale locale, PrinterOption... options);
ProblemPrinterBuilder createProblemPrinterBuilder(Consumer<String> lineConsumer);
}
38 changes: 0 additions & 38 deletions justify/src/main/java/org/leadpony/justify/api/PrinterOption.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2018-2019 the Justify authors.
*
* 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 org.leadpony.justify.api;

import java.util.Locale;

/**
* A builder interface for building a problem printer instance.
*
* @author leadpony
*/
public interface ProblemPrinterBuilder {

/**
* Builds a new instance of problem printer which was configured through this
* interface.
*
* @return newly created instance of problem printer, never be {@code null}.
*/
ProblemHandler build();

/**
* Specifies the target locale for which the messages to present will be
* localized.
*
* <p>
* The default locale obtained via {@link Locale#getDefault()} is used by
* default.
* </p>
*
* @param locale the target locale for which the messages will be localized.
* @return this builder.
* @throws NullPointerException if the specified {@code locale} is {@code null}.
*/
ProblemPrinterBuilder withLocale(Locale locale);

/**
* Specifies whether problem locations should be presented in the form of line
* and column numbers or not.
*
* <p>
* The value of this option is set to {@code true} by default.
* </p>
*
* @param present {@code true} if the locations should be presented,
* {@code false} if the locations should be omitted.
* @return this builder.
*/
ProblemPrinterBuilder withLocation(boolean present);

/**
* Specifies whether problem locations should be presented in the form of a
* JSON pointer or not.
*
* <p>
* The value of this option is set to {@code true} by default.
* </p>
*
* @param present {@code true} if the pointers should be presented,
* {@code false} if the pointers should be omitted.
* @return this builder.
*/
ProblemPrinterBuilder withPointer(boolean present);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,77 @@
*/
package org.leadpony.justify.internal.problem;

import java.util.Arrays;
import java.util.EnumSet;
import static org.leadpony.justify.internal.base.Arguments.requireNonNull;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import javax.json.stream.JsonLocation;

import org.leadpony.justify.api.PrinterOption;
import org.leadpony.justify.api.Problem;
import org.leadpony.justify.api.ProblemHandler;
import org.leadpony.justify.api.ProblemPrinterBuilder;
import org.leadpony.justify.internal.base.Message;

/**
* A factory type for producing problem printers.
* The default implementation of {@link ProblemPrinterBuilder}.
*
* @author leadpony
*/
public class ProblemPrinterFactory {
public class DefaultProblemPrinterBuilder implements ProblemPrinterBuilder {

private static final ProblemFormatter SIMPLE_FORMAT = (problem, locale)->{
return problem.getMessage(locale);
};

private static final ProblemFormatter LOCATION_ONLY_FORMAT = (problem, locale)->{
return Message.LINE_WITH_LOCATION.format(formatArgs(problem, locale), locale);
};

private static final ProblemFormatter POINTER_ONLY_FORMAT = (problem, locale)->{
return Message.LINE_WITH_POINTER.format(formatArgs(problem, locale), locale);
};

private static final ProblemFormatter LOCATION_AND_POINTER_FORMAT = (problem, locale)->{
return Message.LINE_WITH_BOTH.format(formatArgs(problem, locale), locale);
};

private final Consumer<String> lineConsumer;
private Locale locale = Locale.getDefault();
private boolean location = true;
private boolean pointer = true;

public DefaultProblemPrinterBuilder(Consumer<String> lineConsumer) {
this.lineConsumer = lineConsumer;
}

@Override
public ProblemHandler build() {
return new ProblemPrinter(lineConsumer, locale, createFormatter());
}

@Override
public ProblemPrinterBuilder withLocale(Locale locale) {
requireNonNull(locale, "locale");
this.locale = locale;
return this;
}

private static Map<String, Object> args(Problem problem, Locale locale) {
@Override
public ProblemPrinterBuilder withLocation(boolean present) {
this.location = present;
return this;
}

@Override
public ProblemPrinterBuilder withPointer(boolean present) {
this.pointer = present;
return this;
}

private static Map<String, Object> formatArgs(Problem problem, Locale locale) {
Map<String, Object> args = new HashMap<>();
args.put("message", problem.getMessage(locale));
JsonLocation location = problem.getLocation();
Expand All @@ -56,37 +104,14 @@ private static Map<String, Object> args(Problem problem, Locale locale) {
return args;
}

private static final ProblemFormatter SIMPLE_FORMAT = (problem, locale)->{
return problem.getMessage(locale);
};

private static final ProblemFormatter LOCATION_ONLY_FORMAT = (problem, locale)->{
return Message.LINE_WITH_LOCATION.format(args(problem, locale), locale);
};

private static final ProblemFormatter POINTER_ONLY_FORMAT = (problem, locale)->{
return Message.LINE_WITH_POINTER.format(args(problem, locale), locale);
};

private static final ProblemFormatter FULL_FORMAT = (problem, locale)->{
return Message.LINE_WITH_BOTH.format(args(problem, locale), locale);
};

public ProblemHandler createPrinter(Consumer<String> lineConsumer, Locale locale, PrinterOption... options) {
Set<PrinterOption> optionSet = EnumSet.noneOf(PrinterOption.class);
optionSet.addAll(Arrays.asList(options));
ProblemFormatter formatter = createFormatter(optionSet);
return new ProblemPrinter(lineConsumer, locale, formatter);
}

private static ProblemFormatter createFormatter(Set<PrinterOption> options) {
if (options.contains(PrinterOption.INCLUDE_LOCATION)) {
if (options.contains(PrinterOption.INCLUDE_POINTER)) {
return FULL_FORMAT;
private ProblemFormatter createFormatter() {
if (location) {
if (pointer) {
return LOCATION_AND_POINTER_FORMAT;
} else {
return LOCATION_ONLY_FORMAT;
}
} else if (options.contains(PrinterOption.INCLUDE_POINTER)) {
} else if (pointer) {
return POINTER_ONLY_FORMAT;
}
return SIMPLE_FORMAT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
import org.leadpony.justify.api.JsonSchemaReaderFactoryBuilder;
import org.leadpony.justify.api.JsonSchemaResolver;
import org.leadpony.justify.api.JsonValidationService;
import org.leadpony.justify.api.PrinterOption;
import org.leadpony.justify.api.ProblemHandler;
import org.leadpony.justify.api.ProblemHandlerFactory;
import org.leadpony.justify.api.ProblemPrinterBuilder;
import org.leadpony.justify.internal.base.Message;
import org.leadpony.justify.internal.base.json.JsonProviderDecorator;
import org.leadpony.justify.internal.base.json.PointingJsonParser;
import org.leadpony.justify.internal.keyword.assertion.content.ContentAttributeRegistry;
import org.leadpony.justify.internal.keyword.assertion.format.FormatAttributeRegistry;
import org.leadpony.justify.internal.problem.ProblemPrinterFactory;
import org.leadpony.justify.internal.problem.DefaultProblemPrinterBuilder;
import org.leadpony.justify.internal.schema.DefaultSchemaBuilderFactory;
import org.leadpony.justify.internal.schema.io.Draft07SchemaReader;
import org.leadpony.justify.internal.schema.io.DefaultJsonSchemaReaderFactory;
Expand All @@ -75,7 +75,6 @@ class DefaultJsonValidationService implements JsonValidationService, JsonSchemaR
private final ContentAttributeRegistry contentRegistry;
private final JsonSchema metaschema;
private final JsonSchemaReaderFactory defaultSchemaReaderFactory;
private final ProblemPrinterFactory printerFactory = new ProblemPrinterFactory();

private static final String METASCHEMA_NAME = "metaschema-draft-07.json";

Expand Down Expand Up @@ -301,21 +300,27 @@ public JsonProvider createJsonProvider(JsonSchema schema, ProblemHandlerFactory
* {@inheritDoc}
*/
@Override
public ProblemHandler createProblemPrinter(Consumer<String> lineConsumer, Locale locale) {
return createProblemPrinter(lineConsumer, locale,
PrinterOption.INCLUDE_LOCATION,
PrinterOption.INCLUDE_POINTER);
public ProblemHandler createProblemPrinter(Consumer<String> lineConsumer) {
return createProblemPrinter(lineConsumer, Locale.getDefault());
}

/**
* {@inheritDoc}
*/
@Override
public ProblemHandler createProblemPrinter(Consumer<String> lineConsumer, Locale locale, PrinterOption... options) {
public ProblemHandler createProblemPrinter(Consumer<String> lineConsumer, Locale locale) {
requireNonNull(lineConsumer, "lineConsumer");
requireNonNull(locale, "locale");
requireNonNull(options, "options");
return printerFactory.createPrinter(lineConsumer, locale, options);
return createProblemPrinterBuilder(lineConsumer).withLocale(locale).build();
}

/**
* {@inheritDoc}
*/
@Override
public ProblemPrinterBuilder createProblemPrinterBuilder(Consumer<String> lineConsumer) {
requireNonNull(lineConsumer, "lineConsumer");
return new DefaultProblemPrinterBuilder(lineConsumer);
}

/* JsonSchemaResolver interface */
Expand Down
Loading

0 comments on commit 6e3842d

Please sign in to comment.