Skip to content

Commit

Permalink
Typify internal list in DirectGraphLayout and convert to Deque
Browse files Browse the repository at this point in the history
By using a deque, we have access to both an iterator() and an
descendingIterator() method. This allows us to iterate over the
collection in both directions.
  • Loading branch information
ptziegler committed Aug 18, 2023
1 parent 886990a commit ff45033
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
package org.eclipse.draw2d.test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.ArrayDeque;
import java.util.Deque;

import org.eclipse.draw2d.graph.DirectedGraph;
import org.eclipse.draw2d.graph.DirectedGraphLayout;
Expand Down Expand Up @@ -146,10 +147,9 @@ private static DirectedGraphLayout createDirectedGraphLayoutWithSelectedStepOnly
DirectedGraphLayout layout = new DirectedGraphLayout();
Field stepsField = DirectedGraphLayout.class.getDeclaredField("steps"); //$NON-NLS-1$
stepsField.setAccessible(true);
ArrayList steps = (ArrayList) stepsField.get(layout);
ArrayList filteredSteps = new ArrayList();
for (int i = 0; i < steps.size(); i++) {
Object graphVisitor = steps.get(i);
Deque<?> steps = (Deque<?>) stepsField.get(layout);
Deque<Object> filteredSteps = new ArrayDeque<>();
for (Object graphVisitor : steps) {
for (String graphVisitorClassName : graphVisitorClassNames) {
if (graphVisitorClassName.equals(graphVisitor.getClass().getName())) {
filteredSteps.add(graphVisitor);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2003, 2010 IBM Corporation and others.
* Copyright (c) 2003, 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -10,8 +10,8 @@
*******************************************************************************/
package org.eclipse.draw2d.graph;

import java.util.ArrayList;
import java.util.List;
import java.util.ArrayDeque;
import java.util.Deque;

/**
* Performs a graph layout of a <code>DirectedGraph</code>. The directed graph
Expand Down Expand Up @@ -70,7 +70,7 @@
*/
public class DirectedGraphLayout {

List steps = new ArrayList();
Deque<GraphVisitor> steps = new ArrayDeque<>();

/**
* @since 3.1
Expand Down Expand Up @@ -101,14 +101,8 @@ void init() {
public void visit(DirectedGraph graph) {
if (graph.nodes.isEmpty())
return;
for (int i = 0; i < steps.size(); i++) {
GraphVisitor visitor = (GraphVisitor) steps.get(i);
visitor.visit(graph);
}
for (int i = steps.size() - 1; i >= 0; i--) {
GraphVisitor visitor = (GraphVisitor) steps.get(i);
visitor.revisit(graph);
}
steps.iterator().forEachRemaining(visitor -> visitor.visit(graph));
steps.descendingIterator().forEachRemaining(visitor -> visitor.revisit(graph));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2005, CHISEL Group, University of Victoria, Victoria, BC, Canada.
* Copyright 2005, 2023, CHISEL Group, University of Victoria, Victoria, BC, Canada.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution, and is available at
Expand All @@ -10,9 +10,9 @@
package org.eclipse.zest.layouts.algorithms;

import java.lang.reflect.Field;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.graph.DirectedGraph;
Expand All @@ -34,7 +34,7 @@ public void visit(DirectedGraph graph) {
field = DirectedGraphLayout.class.getDeclaredField("steps");
field.setAccessible(true);
Object object = field.get(this);
List steps = (List) object;
Deque<?> steps = (Deque<?>) object;
steps.remove(10);
steps.remove(9);
steps.remove(8);
Expand Down

0 comments on commit ff45033

Please sign in to comment.