diff options
author | Thomas Watson | 2020-07-06 14:44:43 +0000 |
---|---|---|
committer | Thomas Watson | 2020-07-06 14:44:43 +0000 |
commit | 235fa745dd570fa5b264a4f45c4345a33ef577aa (patch) | |
tree | 03566ba2ea920c069874dc85436643ab1ab5d1c7 | |
parent | 17fdf326eff3180a0958de486394f8e079b5d164 (diff) | |
download | rt.equinox.framework-235fa745dd570fa5b264a4f45c4345a33ef577aa.tar.gz rt.equinox.framework-235fa745dd570fa5b264a4f45c4345a33ef577aa.tar.xz rt.equinox.framework-235fa745dd570fa5b264a4f45c4345a33ef577aa.zip |
Update OSGi R8 API to the latest
Change-Id: Ib26d9d161cfd1fcfa8a1c6240bd686e76b778a00
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
-rw-r--r-- | bundles/org.eclipse.osgi/osgi/src/org/osgi/dto/DTO.java | 460 |
1 files changed, 238 insertions, 222 deletions
diff --git a/bundles/org.eclipse.osgi/osgi/src/org/osgi/dto/DTO.java b/bundles/org.eclipse.osgi/osgi/src/org/osgi/dto/DTO.java index 53074a69b..ffcb6416b 100644 --- a/bundles/org.eclipse.osgi/osgi/src/org/osgi/dto/DTO.java +++ b/bundles/org.eclipse.osgi/osgi/src/org/osgi/dto/DTO.java @@ -1,5 +1,5 @@ /* - * Copyright (c) OSGi Alliance (2012, 2017). All Rights Reserved. + * Copyright (c) OSGi Alliance (2012, 2020). All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,9 +28,10 @@ import java.util.Set; * Super type for Data Transfer Objects. * <p> * A Data Transfer Object (DTO) is easily serializable having only public fields - * of primitive types and their wrapper classes, Strings, enums, Version, and + * of primitive types and their wrapper classes, String, enums, Version, and * DTOs. List, Set, Map, and array aggregates may also be used. The aggregates - * must only hold objects of the listed types or aggregates. + * must only hold objects of the listed types or aggregates. The types for Map + * keys are limited to primitive wrapper classes, String, enums, and Version. * <p> * The object graph from a Data Transfer Object must be a tree to simplify * serialization and deserialization. @@ -40,84 +41,87 @@ import java.util.Set; */ public abstract class DTO { - /** - * Return a string representation of this DTO suitable for use when - * debugging. - * - * <p> - * The format of the string representation is not specified and subject to - * change. - * - * @return A string representation of this DTO suitable for use when - * debugging. - */ - @Override - public String toString() { - return appendValue(new StringBuilder(), new IdentityHashMap<Object, String>(), "#", this).toString(); - } + /** + * Return a string representation of this DTO suitable for use when + * debugging. + * <p> + * The format of the string representation is not specified and subject to + * change. + * + * @return A string representation of this DTO suitable for use when + * debugging. + */ + @Override + public String toString() { + return appendValue(new StringBuilder(), + new IdentityHashMap<Object,String>(), "#", this).toString(); + } - /** - * Append the specified DTO's string representation to the specified - * StringBuilder. - * - * @param result StringBuilder to which the string representation is - * appended. - * @param objectRefs References to "seen" objects. - * @param refpath The reference path of the specified DTO. - * @param dto The DTO whose string representation is to be appended. - * @return The specified StringBuilder. - */ - private static StringBuilder appendDTO(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final DTO dto) { - result.append("{"); - String delim = ""; - for (Field field : dto.getClass().getFields()) { - if (Modifier.isStatic(field.getModifiers())) { - continue; - } - result.append(delim); - final String name = field.getName(); - appendString(result, name); - result.append(":"); - Object value = null; - try { - value = field.get(dto); - } catch (IllegalAccessException e) { - // use null value; - } - appendValue(result, objectRefs, refpath + "/" + name, value); - delim = ", "; - } - result.append("}"); - return result; - } + /** + * Append the specified DTO's string representation to the specified + * StringBuilder. + * + * @param result StringBuilder to which the string representation is + * appended. + * @param objectRefs References to "seen" objects. + * @param refpath The reference path of the specified DTO. + * @param dto The DTO whose string representation is to be appended. + * @return The specified StringBuilder. + */ + private static StringBuilder appendDTO(final StringBuilder result, + final Map<Object,String> objectRefs, final String refpath, + final DTO dto) { + result.append('{'); + String delim = ""; + for (Field field : dto.getClass().getFields()) { + if (Modifier.isStatic(field.getModifiers())) { + continue; + } + result.append(delim); + final String name = field.getName(); + appendString(result, name); + result.append(':'); + Object value = null; + try { + value = field.get(dto); + } catch (IllegalAccessException e) { + // use null value; + } + appendValue(result, objectRefs, refpath + "/" + name, value); + delim = ", "; + } + result.append('}'); + return result; + } - /** - * Append the specified value's string representation to the specified - * StringBuilder. - * - * <p> - * This method handles cycles in the object graph, using path-based - * references, even though the specification requires the object graph from - * a DTO to be a tree. - * - * @param result StringBuilder to which the string representation is - * appended. - * @param objectRefs References to "seen" objects. - * @param refpath The reference path of the specified value. - * @param value The object whose string representation is to be appended. - * @return The specified StringBuilder. - */ - private static StringBuilder appendValue(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final Object value) { - if (value == null) { - return result.append("null"); - } - // Simple Java types - if (value instanceof String || value instanceof Character) { - return appendString(result, compress(value.toString())); - } - if (value instanceof Number || value instanceof Boolean) { - return result.append(value.toString()); - } + /** + * Append the specified value's string representation to the specified + * StringBuilder. + * <p> + * This method handles cycles in the object graph, using path-based + * references, even though the specification requires the object graph from + * a DTO to be a tree. + * + * @param result StringBuilder to which the string representation is + * appended. + * @param objectRefs References to "seen" objects. + * @param refpath The reference path of the specified value. + * @param value The object whose string representation is to be appended. + * @return The specified StringBuilder. + */ + private static StringBuilder appendValue(final StringBuilder result, + final Map<Object,String> objectRefs, final String refpath, + final Object value) { + if (value == null) { + return result.append("null"); + } + // Simple Java types + if (value instanceof String || value instanceof Character) { + return appendString(result, compress(value.toString())); + } + if (value instanceof Number || value instanceof Boolean) { + return result.append(value.toString()); + } if (value instanceof Enum) { return appendString(result, ((Enum< ? >) value).name()); } @@ -125,153 +129,165 @@ public abstract class DTO { return appendString(result, value.toString()); } - // Complex types - final String path = objectRefs.get(value); - if (path != null) { - result.append("{\"$ref\":"); - appendString(result, path); - result.append("}"); - return result; - } - objectRefs.put(value, refpath); + // Complex types + final String path = objectRefs.get(value); + if (path != null) { + result.append("{\"$ref\":"); + appendString(result, path); + result.append('}'); + return result; + } + objectRefs.put(value, refpath); - if (value instanceof DTO) { - return appendDTO(result, objectRefs, refpath, (DTO) value); - } - if (value instanceof Map) { - return appendMap(result, objectRefs, refpath, (Map<?, ?>) value); - } - if (value instanceof List || value instanceof Set) { - return appendIterable(result, objectRefs, refpath, (Iterable<?>) value); - } - if (value.getClass().isArray()) { - return appendArray(result, objectRefs, refpath, value); - } - return appendString(result, compress(value.toString())); - } + if (value instanceof DTO) { + return appendDTO(result, objectRefs, refpath, (DTO) value); + } + if (value instanceof Map) { + return appendMap(result, objectRefs, refpath, (Map< ? , ? >) value); + } + if (value instanceof List || value instanceof Set) { + return appendIterable(result, objectRefs, refpath, + (Iterable< ? >) value); + } + if (value.getClass().isArray()) { + return appendArray(result, objectRefs, refpath, value); + } + return appendString(result, compress(value.toString())); + } - /** - * Append the specified array's string representation to the specified - * StringBuilder. - * - * @param result StringBuilder to which the string representation is - * appended. - * @param objectRefs References to "seen" objects. - * @param refpath The reference path of the specified array. - * @param array The array whose string representation is to be appended. - * @return The specified StringBuilder. - */ - private static StringBuilder appendArray(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final Object array) { - result.append("["); - final int length = Array.getLength(array); - for (int i = 0; i < length; i++) { - if (i > 0) { - result.append(","); - } - appendValue(result, objectRefs, refpath + "/" + i, Array.get(array, i)); - } - result.append("]"); - return result; - } + /** + * Append the specified array's string representation to the specified + * StringBuilder. + * + * @param result StringBuilder to which the string representation is + * appended. + * @param objectRefs References to "seen" objects. + * @param refpath The reference path of the specified array. + * @param array The array whose string representation is to be appended. + * @return The specified StringBuilder. + */ + private static StringBuilder appendArray(final StringBuilder result, + final Map<Object,String> objectRefs, final String refpath, + final Object array) { + result.append('['); + final int length = Array.getLength(array); + for (int i = 0; i < length; i++) { + if (i > 0) { + result.append(','); + } + appendValue(result, objectRefs, refpath + "/" + i, + Array.get(array, i)); + } + result.append(']'); + return result; + } - /** - * Append the specified iterable's string representation to the specified - * StringBuilder. - * - * @param result StringBuilder to which the string representation is - * appended. - * @param objectRefs References to "seen" objects. - * @param refpath The reference path of the specified list. - * @param iterable The iterable whose string representation is to be - * appended. - * @return The specified StringBuilder. - */ - private static StringBuilder appendIterable(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final Iterable<?> iterable) { - result.append("["); - int i = 0; - for (Object item : iterable) { - if (i > 0) { - result.append(","); - } - appendValue(result, objectRefs, refpath + "/" + i, item); - i++; - } - result.append("]"); - return result; - } + /** + * Append the specified iterable's string representation to the specified + * StringBuilder. + * + * @param result StringBuilder to which the string representation is + * appended. + * @param objectRefs References to "seen" objects. + * @param refpath The reference path of the specified list. + * @param iterable The iterable whose string representation is to be + * appended. + * @return The specified StringBuilder. + */ + private static StringBuilder appendIterable(final StringBuilder result, + final Map<Object,String> objectRefs, final String refpath, + final Iterable< ? > iterable) { + result.append('['); + int i = 0; + for (Object item : iterable) { + if (i > 0) { + result.append(','); + } + appendValue(result, objectRefs, refpath + "/" + i, item); + i++; + } + result.append(']'); + return result; + } - /** - * Append the specified map's string representation to the specified - * StringBuilder. - * - * @param result StringBuilder to which the string representation is - * appended. - * @param objectRefs References to "seen" objects. - * @param refpath The reference path of the specified map. - * @param map The map whose string representation is to be appended. - * @return The specified StringBuilder. - */ - private static StringBuilder appendMap(final StringBuilder result, final Map<Object, String> objectRefs, final String refpath, final Map<?, ?> map) { - result.append("{"); - String delim = ""; - for (Map.Entry<?, ?> entry : map.entrySet()) { - result.append(delim); - final String name = String.valueOf(entry.getKey()); - appendString(result, name); - result.append(":"); - final Object value = entry.getValue(); - appendValue(result, objectRefs, refpath + "/" + name, value); - delim = ", "; - } - result.append("}"); - return result; - } + /** + * Append the specified map's string representation to the specified + * StringBuilder. + * + * @param result StringBuilder to which the string representation is + * appended. + * @param objectRefs References to "seen" objects. + * @param refpath The reference path of the specified map. + * @param map The map whose string representation is to be appended. + * @return The specified StringBuilder. + */ + private static StringBuilder appendMap(final StringBuilder result, + final Map<Object,String> objectRefs, final String refpath, + final Map< ? , ? > map) { + result.append('{'); + String delim = ""; + for (Map.Entry< ? , ? > entry : map.entrySet()) { + result.append(delim); + final String name = String.valueOf(entry.getKey()); + appendString(result, name); + result.append(':'); + final Object value = entry.getValue(); + appendValue(result, objectRefs, refpath + "/" + name, value); + delim = ", "; + } + result.append('}'); + return result; + } - /** - * Append the specified string to the specified StringBuilder. - * - * @param result StringBuilder to which the string is appended. - * @param string The string to be appended. - * @return The specified StringBuilder. - */ - private static StringBuilder appendString(final StringBuilder result, final CharSequence string) { - result.append("\""); - int i = result.length(); - result.append(string); - while (i < result.length()) { // escape if necessary - char c = result.charAt(i); - if ((c == '"') || (c == '\\')) { - result.insert(i, '\\'); - i = i + 2; - continue; - } - if (c < 0x20) { - result.insert(i + 1, Integer.toHexString(c | 0x10000)); - result.replace(i, i + 2, "\\u"); - i = i + 6; - continue; - } - i++; - } - result.append("\""); - return result; - } + /** + * Append the specified string to the specified StringBuilder. + * + * @param result StringBuilder to which the string is appended. + * @param string The string to be appended. + * @return The specified StringBuilder. + */ + private static StringBuilder appendString(final StringBuilder result, + final CharSequence string) { + result.append('"'); + int i = result.length(); + result.append(string); + while (i < result.length()) { // escape if necessary + char c = result.charAt(i); + if ((c == '"') || (c == '\\')) { + result.insert(i, '\\'); + i = i + 2; + continue; + } + if (c < 0x20) { + result.insert(i + 1, Integer.toHexString(c | 0x10000)); + result.replace(i, i + 2, "\\u"); + i = i + 6; + continue; + } + i++; + } + result.append('"'); + return result; + } - /** - * Compress, in length, the specified string. - * - * @param in The string to potentially compress. - * @return The string compressed, if necessary. - */ - private static CharSequence compress(final CharSequence in) { - final int length = in.length(); - if (length <= 21) { - return in; - } - StringBuilder result = new StringBuilder(21); - result.append(in, 0, 9); - result.append("..."); - result.append(in, length - 9, length); - return result; - } + private static final int MAX_LENGTH = 100; + /** + * Compress, in length, the specified string. + * + * @param in The string to potentially compress. + * @return The string compressed, if necessary. + */ + private static CharSequence compress(final CharSequence in) { + final int length = in.length(); + if (length <= MAX_LENGTH) { + return in; + } + StringBuilder result = new StringBuilder(MAX_LENGTH) + .append(in, 0, MAX_LENGTH / 2 - 3) + .append('.') + .append('.') + .append('.') + .append(in, length - (MAX_LENGTH / 2), length); + return result; + } } |