Skip to content

Commit

Permalink
Improve code readability (code style)
Browse files Browse the repository at this point in the history
- Create arrays using curly braces
- Use diamond operator in generics
- Convert iterators to enhanced for-loops
- Convert v.substring(idx, v.length()) to v.substring(idx)
  • Loading branch information
carlosame committed Sep 14, 2023
1 parent ae3f3de commit b4cedaa
Show file tree
Hide file tree
Showing 30 changed files with 47 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ public void dispose() {
}
}

Iterator<Entry<String, Sandwich>> cssIt = info.cssAnimations.entrySet().iterator();
while (cssIt.hasNext()) {
Entry<String, Sandwich> e2 = cssIt.next();
for (Entry<String, Sandwich> e2 : info.cssAnimations.entrySet()) {
String propertyName = e2.getKey();
Sandwich sandwich = e2.getValue();
if (sandwich.listenerRegistered) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public float distanceTo3(AnimatableValue other) {
*/
@Override
public AnimatableValue getZeroValue() {
return new AnimatableTransformListValue(target, new Vector<SVGTransform>(5));
return new AnimatableTransformListValue(target, new Vector<>(5));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ public void dump() {
*/
public RectListManager(Collection<?> rects) {
this.rects = new Rectangle[rects.size()];
Iterator<?> i = rects.iterator();
int j = 0;
while (i.hasNext()) { // todo can be replaced by rects.toArray()
Object o = i.next();
for (Object o : rects) { // todo can be replaced by rects.toArray()
if (!(o instanceof Rectangle)) {
throw new IllegalArgumentException("Collection must have only rectangles.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ public int size() {
}

public SegmentList.SplitResults split(double y) {
Iterator<Segment> iter = segments.iterator();
SegmentList above = new SegmentList();
SegmentList below = new SegmentList();
while (iter.hasNext()) {
Segment seg = iter.next();
for (Segment seg : segments) {
Segment.SplitResults results = seg.split(y);
if (results == null) {
Rectangle2D bounds = seg.getBounds2D();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ public String[] getPropertyNames() {
ret[i++] = iter.next();
}

Iterator<RenderableImage> rIter = srcs.iterator();
while (rIter.hasNext()) {
RenderableImage ri = rIter.next();
for (RenderableImage ri : srcs) {
String[] srcProps = ri.getPropertyNames();
if (srcProps.length != 0) {
String[] tmp = new String[ret.length + srcProps.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int[] getRgb(int[] rgb) {
* in half to generate two cubes.
*/
private static class Cube {
static final byte[] RGB_BLACK = new byte[] { 0, 0, 0 };
static final byte[] RGB_BLACK = { 0, 0, 0 };

int[] min = { 0, 0, 0 }, max = { 255, 255, 255 };

Expand Down Expand Up @@ -666,7 +666,7 @@ static List<Counter>[] createColorList(BufferedImage bi) {
static Counter[][] convertColorList(List<Counter>[] colors) {

// used to fill empty slots
final Counter[] EMPTY_COUNTER = new Counter[0];
final Counter[] EMPTY_COUNTER = {};

Counter[][] colorTbl = new Counter[1 << 12][];
for (int i = 0; i < colors.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Filter handleURL(ParsedURL purl, boolean needRawData) {

final DeferRable dr = new DeferRable();
final String errCode = ERR_URL_FORMAT_UNREADABLE;
final Object[] errParam = new Object[] { "JDK", url };
final Object[] errParam = { "JDK", url };

Thread t = new Thread() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2017,9 +2017,7 @@ public static synchronized List<BridgeExtension> getGlobalBridgeExtensions() {

ServiceLoader<BridgeExtension> loader = ServiceLoader.load(BridgeExtension.class);

Iterator<BridgeExtension> iter = loader.iterator();
while (iter.hasNext()) {
BridgeExtension be = iter.next();
for (BridgeExtension be : loader) {
float priority = be.getPriority();
ListIterator<BridgeExtension> li = globalExtensions.listIterator();
for (;;) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Filter getBrokenLinkImage(Object base, String code, Object[] params) {
shapeNode.setShape(new java.awt.Rectangle(100, 100));
TextNode textNode = new TextNode();
AttributedString atts = new AttributedString(message);
atts.addAttribute(StrokingTextPainter.GVT_FONTS, new LinkedList<GVTFont>());
atts.addAttribute(StrokingTextPainter.GVT_FONTS, new LinkedList<>());
atts.addAttribute(TextAttribute.SIZE, 14f);
textNode.setAttributedCharacterIterator(atts.getIterator());
cgn.add(shapeNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package io.sf.carte.echosvg.bridge;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -118,11 +117,8 @@ public static GVTFontFamily getFontFamily(Element textElement, BridgeContext ctx
fontFamilyMap.put(doc, fontFaces);
}

Iterator<FontFace> iter = fontFaces.iterator();
List<GVTFontFamily> svgFontFamilies = new LinkedList<>();
while (iter.hasNext()) {
FontFace fontFace = iter.next();

for (FontFace fontFace : fontFaces) {
if (!fontFace.hasFamilyName(fontFamilyName)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1154,17 +1154,13 @@ public AttributedString toAttributedString() {

// Set the attributes

Iterator<String> sit = strings.iterator();
Iterator<Map<Attribute, Object>> ait = attributes.iterator();
int idx = 0;
while (sit.hasNext()) {
String s = sit.next();
for (String s : strings) {
int nidx = idx + s.length();
Map<Attribute, Object> m = ait.next();
Iterator<Attribute> kit = m.keySet().iterator();
Iterator<?> vit = m.values().iterator();
while (kit.hasNext()) {
Attribute attr = kit.next();
for (Attribute attr : m.keySet()) {
Object val = vit.next();
result.addAttribute(attr, val, idx, nidx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1377,12 +1377,10 @@ public int[] getSelected(Mark startMark, Mark finishMark) {

// get the list of text runs
List<TextRun> textRuns = getTextRuns(textNode, aci);
Iterator<TextRun> trI = textRuns.iterator();
int startGlyphIndex = -1;
int endGlyphIndex = -1;
TextSpanLayout startLayout = null, endLayout = null;
while (trI.hasNext()) {
TextRun tr = trI.next();
for (TextRun tr : textRuns) {
TextSpanLayout tsl = tr.getLayout();
if (startGlyphIndex == -1) {
startGlyphIndex = tsl.getGlyphIndex(result[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public static AffineTransform getPreserveAspectRatioTransform(Element e, SVGAnim
return new AffineTransform();
}
SVGRect viewBox = aViewBox.getAnimVal();
float[] vb = new float[] { viewBox.getX(), viewBox.getY(), viewBox.getWidth(), viewBox.getHeight() };
float[] vb = { viewBox.getX(), viewBox.getY(), viewBox.getWidth(), viewBox.getHeight() };

return getPreserveAspectRatioTransform(e, vb, w, h, aPAR, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class WindowWrapper extends ImporterTopLevel {

private static final long serialVersionUID = 1L;

private static final Object[] EMPTY_ARGUMENTS = new Object[0];
private static final Object[] EMPTY_ARGUMENTS = {};

/**
* The rhino interpreter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,15 @@ public ContentManager(XBLOMShadowTreeElement s, XBLManager xm) {
public void dispose() {
xblManager.setContentManager(shadowTree, null);

Iterator<Entry<Node, NodeList>> i = selectedNodes.entrySet().iterator();
while (i.hasNext()) {
Entry<Node, NodeList> e = i.next();
for (Entry<Node, NodeList> e : selectedNodes.entrySet()) {
NodeList nl = e.getValue();
for (int j = 0; j < nl.getLength(); j++) {
Node n = nl.item(j);
xblManager.getRecord(n).contentElement = null;
}
}

Iterator<SVGOMElement> celIt = contentElementList.iterator();
while (celIt.hasNext()) {
NodeEventTarget n = celIt.next();
for (NodeEventTarget n : contentElementList) {
n.removeEventListenerNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, "DOMAttrModified",
contentElementDomAttrModifiedEventListener, false);
}
Expand Down Expand Up @@ -296,9 +292,7 @@ protected void update(boolean first) {
}
}

Iterator<SVGOMElement> netIt = contentElementList.iterator();
while (netIt.hasNext()) {
NodeEventTarget n = netIt.next();
for (NodeEventTarget n : contentElementList) {
n.removeEventListenerNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, "DOMAttrModified",
contentElementDomAttrModifiedEventListener, false);
}
Expand All @@ -325,8 +319,7 @@ protected void update(boolean first) {
}
}

HashSet<Node> removed = new HashSet<>();
removed.addAll(previouslySelectedNodes);
HashSet<Node> removed = new HashSet<>(previouslySelectedNodes);
removed.removeAll(newlySelectedNodes);

HashSet<Node> added = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public static CSSStylableElement getParentCSSStylableElement(Element elt) {
/**
* The listeners.
*/
protected List<CSSEngineListener> listeners = Collections.synchronizedList(new LinkedList<CSSEngineListener>());
protected List<CSSEngineListener> listeners = Collections.synchronizedList(new LinkedList<>());

/**
* The attributes found in stylesheets selectors.
Expand Down Expand Up @@ -1919,7 +1919,7 @@ private void throwUnsupportedEx() {

// CSS events /////////////////////////////////////////////////////////

protected static final CSSEngineListener[] LISTENER_ARRAY = new CSSEngineListener[0];
protected static final CSSEngineListener[] LISTENER_ARRAY = {};

/**
* Adds a CSS engine listener.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public StringMap getIdentifiers() {
}

private DOMException createInvalidRGBComponentUnitDOMException(LexicalType lexicalType) {
Object[] p = new Object[] { getPropertyName(), lexicalType.toString() };
Object[] p = { getPropertyName(), lexicalType.toString() };
String s = Messages.formatMessage("invalid.rgb.component.unit", p);
return new DOMException(DOMException.NOT_SUPPORTED_ERR, s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public String getSeparator() throws DOMException {
* Creates an INVALID_ACCESS_ERR exception.
*/
protected DOMException createDOMException() {
Object[] p = new Object[] { (int) getCssValueType() };
Object[] p = { (int) getCssValueType() };
String s = Messages.formatMessage("invalid.value.access", p);
return new DOMException(DOMException.INVALID_ACCESS_ERR, s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected static String resolveURI(ParsedURL base, String value) {
* Creates a DOM exception, given an invalid identifier.
*/
protected DOMException createInvalidIdentifierDOMException(String ident) {
Object[] p = new Object[] { getPropertyName(), ident };
Object[] p = { getPropertyName(), ident };
String s = Messages.formatMessage("invalid.identifier", p);
return new DOMException(DOMException.SYNTAX_ERR, s);
}
Expand All @@ -57,7 +57,7 @@ protected DOMException createInvalidIdentifierDOMException(String ident) {
* Creates a DOM exception, given an invalid lexical unit type.
*/
protected DOMException createInvalidLexicalUnitDOMException(LexicalUnit.LexicalType type) {
Object[] p = new Object[] { getPropertyName(), type.toString() };
Object[] p = { getPropertyName(), type.toString() };
String s = Messages.formatMessage("invalid.lexical.unit", p);
return new DOMException(DOMException.NOT_SUPPORTED_ERR, s);
}
Expand All @@ -66,7 +66,7 @@ protected DOMException createInvalidLexicalUnitDOMException(LexicalUnit.LexicalT
* Creates a DOM exception, given an invalid float type.
*/
protected DOMException createInvalidFloatTypeDOMException(short t) {
Object[] p = new Object[] { getPropertyName(), (int) t };
Object[] p = { getPropertyName(), (int) t };
String s = Messages.formatMessage("invalid.float.type", p);
return new DOMException(DOMException.INVALID_ACCESS_ERR, s);
}
Expand All @@ -75,7 +75,7 @@ protected DOMException createInvalidFloatTypeDOMException(short t) {
* Creates a DOM exception, given an invalid float value.
*/
protected DOMException createInvalidFloatValueDOMException(float f) {
Object[] p = new Object[] { getPropertyName(), f };
Object[] p = { getPropertyName(), f };
String s = Messages.formatMessage("invalid.float.value", p);
return new DOMException(DOMException.INVALID_ACCESS_ERR, s);
}
Expand All @@ -84,19 +84,19 @@ protected DOMException createInvalidFloatValueDOMException(float f) {
* Creates a DOM exception, given an invalid string type.
*/
protected DOMException createInvalidStringTypeDOMException(short t) {
Object[] p = new Object[] { getPropertyName(), (int) t };
Object[] p = { getPropertyName(), (int) t };
String s = Messages.formatMessage("invalid.string.type", p);
return new DOMException(DOMException.INVALID_ACCESS_ERR, s);
}

protected DOMException createMalformedLexicalUnitDOMException() {
Object[] p = new Object[] { getPropertyName() };
Object[] p = { getPropertyName() };
String s = Messages.formatMessage("malformed.lexical.unit", p);
return new DOMException(DOMException.INVALID_ACCESS_ERR, s);
}

protected DOMException createDOMException() {
Object[] p = new Object[] { getPropertyName() };
Object[] p = { getPropertyName() };
String s = Messages.formatMessage("invalid.access", p);
return new DOMException(DOMException.NOT_SUPPORTED_ERR, s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected int getOrientation() {
}

private DOMException createMalformedRectDOMException() {
Object[] p = new Object[] { getPropertyName() };
Object[] p = { getPropertyName() };
String s = Messages.formatMessage("malformed.rect", p);
return new DOMException(DOMException.SYNTAX_ERR, s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void insertData(int offset, String arg) throws DOMException {
throw createDOMException(DOMException.INDEX_SIZE_ERR, "offset", new Object[] { offset });
}
String v = getNodeValue();
setNodeValue(v.substring(0, offset) + arg + v.substring(offset, v.length()));
setNodeValue(v.substring(0, offset) + arg + v.substring(offset));
}

/**
Expand All @@ -148,7 +148,7 @@ public void deleteData(int offset, int count) throws DOMException {
checkOffsetCount(offset, count);

String v = getNodeValue();
setNodeValue(v.substring(0, offset) + v.substring(Math.min(v.length(), offset + count), v.length()));
setNodeValue(v.substring(0, offset) + v.substring(Math.min(v.length(), offset + count)));
}

/**
Expand All @@ -164,7 +164,7 @@ public void replaceData(int offset, int count, String arg) throws DOMException {
checkOffsetCount(offset, count);

String v = getNodeValue();
setNodeValue(v.substring(0, offset) + arg + v.substring(Math.min(v.length(), offset + count), v.length()));
setNodeValue(v.substring(0, offset) + arg + v.substring(Math.min(v.length(), offset + count)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ protected static synchronized List<DomExtension> getDomExtensions() {

ServiceLoader<DomExtension> loader = ServiceLoader.load(DomExtension.class);

Iterator<DomExtension> iter = loader.iterator();

while (iter.hasNext()) {
DomExtension de = iter.next();
for (DomExtension de : loader) {
float priority = de.getPriority();
ListIterator<DomExtension> li = extensions.listIterator();
for (;;) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import java.awt.image.BufferedImage;
import java.text.CharacterIterator;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

Expand Down Expand Up @@ -104,7 +103,7 @@ public class JGVTComponent extends JComponent {
* The GVT tree renderer listeners.
*/
protected List<GVTTreeRendererListener> gvtTreeRendererListeners = Collections
.synchronizedList(new LinkedList<GVTTreeRendererListener>());
.synchronizedList(new LinkedList<>());

/**
* Whether a render was requested.
Expand Down Expand Up @@ -610,10 +609,8 @@ public void setRenderingTransform(AffineTransform at, boolean performRedraw) {
}
}
if (jgvtListeners != null) {
Iterator<JGVTComponentListener> iter = jgvtListeners.iterator();
ComponentEvent ce = new ComponentEvent(this, JGVTComponentListener.COMPONENT_TRANSFORM_CHANGED);
while (iter.hasNext()) {
JGVTComponentListener l = iter.next();
for (JGVTComponentListener l : jgvtListeners) {
l.componentTransformChanged(ce);
}
}
Expand Down
Loading

0 comments on commit b4cedaa

Please sign in to comment.