Skip to content

Commit

Permalink
Converter never throws an IOException when it converts from/to a Char…
Browse files Browse the repository at this point in the history
…Buffer.
  • Loading branch information
Werner Randelshofer committed Feb 21, 2024
1 parent 1a9b831 commit e6f3f72
Show file tree
Hide file tree
Showing 100 changed files with 451 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.beans.property.ReadOnlyListProperty;
import javafx.beans.property.ReadOnlyListWrapper;
import javafx.beans.property.ReadOnlyMapProperty;
import javafx.beans.property.ReadOnlyMapWrapper;
import javafx.beans.property.ReadOnlySetProperty;
Expand Down Expand Up @@ -59,7 +60,7 @@ public abstract class AbstractApplication extends javafx.application.Application

private final @NonNull ReadOnlyMapProperty<String, Action> actions = new ReadOnlyMapWrapper<String, Action>(this, ACTIONS_PROPERTY, FXCollections.observableMap(new LinkedHashMap<>())).getReadOnlyProperty();
private final @NonNull ReadOnlySetProperty<Activity> activities = new ReadOnlySetWrapper<Activity>(this, ACTIVITIES_PROPERTY, FXCollections.observableSet(new LinkedHashSet<>())).getReadOnlyProperty();
private final @NonNull ReadOnlyListProperty<String> stylesheets = new javafx.beans.property.ReadOnlyListWrapper<String>(this, STYLESHEETS_PROPERTY, FXCollections.observableArrayList()).getReadOnlyProperty();
private final @NonNull ReadOnlyListProperty<String> stylesheets = new ReadOnlyListWrapper<String>(this, STYLESHEETS_PROPERTY, FXCollections.observableArrayList()).getReadOnlyProperty();

private final @NonNull ObjectProperty<Supplier<Activity>> activityFactory = new SimpleObjectProperty<>(this, ACTIVITY_FACTORY_PROPERTY);
private final @NonNull ObjectProperty<Supplier<MenuBar>> menuFactory = new SimpleObjectProperty<>(this, MENU_BAR_FACTORY_PROPERTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public Object getModule() {
*
* @param baseName the base name
* @return the resource bundle
* @see java.util.ResourceBundle
* @see ResourceBundle
*/
public static @NonNull Resources getResources(@NonNull String baseName)
throws MissingResourceException {
Expand All @@ -174,7 +174,7 @@ public Object getModule() {
* @param baseName the base name
* @param locale the locale
* @return the resource bundle
* @see java.util.ResourceBundle
* @see ResourceBundle
*/
static Resources getResources(@NonNull String baseName, @NonNull Locale locale)
throws MissingResourceException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ default void configureToolBarButton(@NonNull ButtonBase button, String argument,
boolean containsKey(String key);

/**
* Returns a formatted string using {@link java.util.Formatter}.
* Returns a formatted string using {@link Formatter}.
*
* @param key the key
* @param arguments the arguments
Expand All @@ -201,7 +201,7 @@ default void configureToolBarButton(@NonNull ButtonBase button, String argument,
}

/**
* Returns a formatted string using {@link java.text.MessageFormat}.
* Returns a formatted string using {@link MessageFormat}.
*
* @param key the key
* @param arguments the arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public CaseInsensitiveMappedConverter(@NonNull Map<String, E> fromStringMap) {
}

@Override
public @Nullable E fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException, IOException {
public @Nullable E fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException {
if (in == null) {
throw new ParseException("Illegal value=null", 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public interface Converter<T> {
* @param idResolver The factory for looking up object ids. Nullable for some
* converters.
* @return The value. Nullable.
* @throws ParseException if conversion failed. The error offset field is
* set to the position where parsing failed. The position of the buffer is
* undefined.
* @throws java.io.IOException Thrown by the CharBuffer.
* @throws ParseException if conversion failed. The error offset field is
* set to the position where parsing failed. The position of the buffer is
* undefined.
* @throws IOException Thrown by the CharBuffer.
*/
default @Nullable T fromString(@NonNull CharSequence in, @Nullable IdResolver idResolver) throws ParseException, IOException {
default @Nullable T fromString(@NonNull CharSequence in, @Nullable IdResolver idResolver) throws ParseException {
return fromString(CharBuffer.wrap(in), idResolver);
}

Expand All @@ -60,13 +60,13 @@ public interface Converter<T> {
* @param idResolver The factory for looking up object ids. Nullable for non-resolving
* converters.
* @return The value. Nullable.
* @throws ParseException if conversion failed. The error offset field is
* set to the position where parsing failed. The position of the buffer is
* undefined.
* @throws java.io.IOException Thrown by the CharBuffer.
* @throws ParseException if conversion failed. The error offset field is
* set to the position where parsing failed. The position of the buffer is
* undefined.
* @throws IOException Thrown by the CharBuffer.
*/
@Nullable
T fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException, IOException;
T fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException;

/**
* Converts a value to a string and appends it to the provided
Expand All @@ -79,7 +79,7 @@ public interface Converter<T> {
* @param idSupplier The factory for creating object ids. Nullable for non-resolving
* converters.
* @param value The value. Nullable.
* @throws java.io.IOException thrown by Appendable
* @throws IOException thrown by Appendable
*/
<TT extends T> void toString(Appendable out, @Nullable IdSupplier idSupplier, @Nullable TT value) throws IOException;

Expand All @@ -96,12 +96,12 @@ public interface Converter<T> {
* beginning of the string when this method is invoked. After completion of
* this method, the position is set after the last consumed character.
* @return The value. Nullable.
* @throws ParseException if conversion failed. The error offset field is
* set to the position where parsing failed. The position of the buffer is
* undefined.
* @throws java.io.IOException Thrown by the CharBuffer.
* @throws ParseException if conversion failed. The error offset field is
* set to the position where parsing failed. The position of the buffer is
* undefined.
* @throws IOException Thrown by the CharBuffer.
*/
default @Nullable T fromString(@NonNull CharBuffer in) throws ParseException, IOException {
default @Nullable T fromString(@NonNull CharBuffer in) throws ParseException {
return fromString(in, null);
}

Expand All @@ -121,7 +121,7 @@ public interface Converter<T> {
* @throws ParseException on conversion failure
* @throws IOException on IO failure
*/
default @Nullable T fromString(@NonNull CharSequence in) throws ParseException, IOException {
default @Nullable T fromString(@NonNull CharSequence in) throws ParseException {
CharBuffer buf = CharBuffer.wrap(in);
T value = fromString(buf);
if (buf.remaining() != 0) {
Expand Down Expand Up @@ -184,7 +184,7 @@ public interface Converter<T> {
* @param <TT> the value type
* @param out The appendable
* @param value The value. Nullable.
* @throws java.io.IOException thrown by Appendable
* @throws IOException thrown by Appendable
*/
default <TT extends T> void toString(@NonNull Appendable out, @Nullable TT value) throws IOException {
toString(out, null, value);
Expand Down Expand Up @@ -212,6 +212,28 @@ default <TT extends T> void toString(@NonNull Appendable out, @Nullable TT value
return out.toString();
}

/**
* Converts a value to a String.
* <p>
* This method does not change the state of the converter.
* <p>
* Note: this is a convenience method. Implementing classes rarely need to
* overwrite this method.
*
* @param <TT> the value type
* @param value The value. Nullable.
* @return The String.
*/
default @NonNull <TT extends T> String toString(@Nullable IdSupplier idSupplier, @Nullable TT value) {
StringBuilder out = new StringBuilder();
try {
toString(out, idSupplier, value);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return out.toString();
}

default boolean needsIdResolver() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DefaultConverter() {
}

@Override
public @Nullable Object fromString(@NonNull CharBuffer buf, @Nullable IdResolver idResolver) throws ParseException, IOException {
public @Nullable Object fromString(@NonNull CharBuffer buf, @Nullable IdResolver idResolver) throws ParseException {
String str = buf.toString();
buf.position(buf.limit());
return "null".equals(str) ? null : str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DoubleConverter(boolean nullable, DecimalFormat decimalFormat, DecimalFor
}

@Override
public @Nullable Double fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException, IOException {
public @Nullable Double fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException {
try {
if (in.isEmpty() && nullable) return null;
var result = Double.parseDouble(in.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public FloatConverter(boolean nullable) {
}

@Override
public @Nullable Float fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException, IOException {
public @Nullable Float fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException {
try {
if (in.isEmpty() && nullable) return null;
var result = Float.parseFloat(in.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.jhotdraw8.annotation.NonNull;
import org.jhotdraw8.annotation.Nullable;

import java.io.IOException;
import java.io.Serial;
import java.nio.CharBuffer;
import java.text.FieldPosition;
Expand Down Expand Up @@ -49,8 +48,6 @@ public FormatConverterAdapter(Converter<?> converter) {
} catch (ParseException ex) {
pos.setErrorIndex(ex.getErrorOffset());
return null;
} catch (IOException ex) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public interface IdResolver {
* Gets the object for the specified id. Returns null if the id has no
* object.
*
* @param id the id
* @return the object
* @param id the id or null
* @return the object if present
*/
@Nullable Object getObject(@Nullable String id);

Expand All @@ -24,8 +24,8 @@ public interface IdResolver {
* Returns null if the id has no object of this type.
*
* @param clazz the clazz
* @param id the id
* @return the object
* @param id the id or null
* @return the object if present with the specified type
*/
default <T> @Nullable T getObject(@NonNull Class<T> clazz, @Nullable String id) {
Object object = getObject(id);
Expand All @@ -47,5 +47,4 @@ public interface IdResolver {
* @return an internal URI (typically an absolute path)
*/
@NonNull URI absolutize(@NonNull URI uri);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IntegerConverter(boolean nullable) {
}

@Override
public @Nullable Integer fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException, IOException {
public @Nullable Integer fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException {
try {
if (in.isEmpty() && nullable) return null;
var result = Integer.parseInt(in.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public MappedConverter(@NonNull Map<String, E> fromStringMap, String nullValue)
}

@Override
public @Nullable E fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException, IOException {
public @Nullable E fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException {
if (in == null) {
throw new ParseException("Illegal value=null", 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ public int nextToken() throws IOException {
* tokenizer.
*
* @return the next char
* @throws java.io.IOException in case of an IO error
* @throws IOException in case of an IO error
*/
public int nextChar() throws IOException {
if (pushedBack) {
Expand All @@ -1002,7 +1002,7 @@ public int nextChar() throws IOException {
* Unreads a character back into the input stream of the tokenizer.
*
* @param ch The character
* @throws java.io.IOException in case of an IO error
* @throws IOException in case of an IO error
*/
public void pushCharBack(int ch) throws IOException {
if (pushedBack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public float getMaxValue(int component) {
r = g = b = value;
} else {
float h = (float) (hue - Math.floor(hue)) * 6;
float f = h - (float) java.lang.Math.floor(h);
float f = h - (float) Math.floor(h);
float p = value * (1f - saturation);
float q = value * (1f - saturation * f);
float t = value * (1f - (saturation * (1f - f)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ public StringOrIdentCssConverter() {
}

@Override
public @Nullable String fromString(@NonNull CharBuffer buf, @Nullable IdResolver idResolver) throws ParseException, IOException {
public @Nullable String fromString(@NonNull CharBuffer buf, @Nullable IdResolver idResolver) throws ParseException {
StreamCssTokenizer tt = new StreamCssTokenizer(new CharBufferReader(buf), null);
if (tt.next() != CssTokenType.TT_STRING && tt.current() != CssTokenType.TT_IDENT) {
throw new ParseException("Css String or Ident expected. " + tt.current(), buf.position());
try {
if (tt.next() != CssTokenType.TT_STRING && tt.current() != CssTokenType.TT_IDENT) {
throw new ParseException("Css String or Ident expected. " + tt.current(), buf.position());
}
} catch (IOException e) {
ParseException parseException = new ParseException(e.getMessage(), 0);
parseException.initCause(e);
throw parseException;
}
return tt.currentString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void toString(@NonNull Appendable out, @Nullable IdSupplier idSupplier, @
}

@Override
public @NonNull String fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException, IOException {
public @NonNull String fromString(@NonNull CharBuffer in, @Nullable IdResolver idResolver) throws ParseException {
int pos = in.position();
StringBuilder out = new StringBuilder();
while (in.remaining() > 0 && !Character.isWhitespace(in.charAt(0))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* This abstract class can be extended to implement an {@code Action} that acts
* on behalf of the selected figures of a
* {@link org.jhotdraw8.draw.DrawingView}.
* {@link DrawingView}.
* <p>
* By default the disabled state of this action reflects the disabled state of
* the active {@code DrawingView}. If no drawing view is active, this action is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* A <em>constrainer</em> constrains editing operations performed by
* {@link org.jhotdraw8.draw.tool.Tool}s and
* {@link org.jhotdraw8.draw.handle.Handle}s on a
* {@link org.jhotdraw8.draw.DrawingView}.
* {@link DrawingView}.
* <p>
* {@code Constrainer} objects are associated to {@code DrawingView}'s.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.jhotdraw8.draw.css.converter.ColorCssConverter;
import org.jhotdraw8.draw.render.RenderContext;

import java.io.IOException;
import java.text.ParseException;
import java.util.Objects;

Expand Down Expand Up @@ -136,7 +135,7 @@ public boolean equals(@Nullable Object obj) {
public static @NonNull CssColor valueOf(@NonNull String value) {
try {
return converter.fromString(value);
} catch (ParseException | IOException e) {
} catch (ParseException e) {
return new NamedCssColor(value, Color.BLACK);
}
}
Expand Down
Loading

0 comments on commit e6f3f72

Please sign in to comment.