Resolves checkstyle errors for intercepting-filter, interpreter, iterator (#1065)
* Reduces checkstyle errors in intercepting-filter * Reduces checkstyle errors in interpreter * Reduces checkstyle errors in iterator
This commit is contained in:
committed by
Ilkka Seppälä
parent
dda09535e6
commit
7f06f3b78c
@ -25,13 +25,13 @@ package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Base class for order processing filters. Handles chain management.
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractFilter implements Filter {
|
||||
|
||||
private Filter next;
|
||||
|
||||
public AbstractFilter() {}
|
||||
public AbstractFilter() {
|
||||
}
|
||||
|
||||
public AbstractFilter(Filter next) {
|
||||
this.next = next;
|
||||
|
@ -26,9 +26,8 @@ package com.iluwatar.intercepting.filter;
|
||||
/**
|
||||
* Concrete implementation of filter This filter is responsible for checking/filtering the input in
|
||||
* the address field.
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public class AddressFilter extends AbstractFilter {
|
||||
|
||||
|
@ -24,7 +24,6 @@
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
*
|
||||
* When a request enters a Web application, it often must pass several entrance tests prior to the
|
||||
* main processing stage. For example, - Has the client been authenticated? - Does the client have a
|
||||
* valid session? - Is the client's IP address from a trusted network? - Does the request path
|
||||
@ -32,28 +31,26 @@ package com.iluwatar.intercepting.filter;
|
||||
* the browser type of the client? Some of these checks are tests, resulting in a yes or no answer
|
||||
* that determines whether processing will continue. Other checks manipulate the incoming data
|
||||
* stream into a form suitable for processing.
|
||||
* <p>
|
||||
* The classic solution consists of a series of conditional checks, with any failed check aborting
|
||||
* the request. Nested if/else statements are a standard strategy, but this solution leads to code
|
||||
* fragility and a copy-and-paste style of programming, because the flow of the filtering and the
|
||||
* action of the filters is compiled into the application.
|
||||
* <p>
|
||||
* The key to solving this problem in a flexible and unobtrusive manner is to have a simple
|
||||
*
|
||||
* <p>The classic solution consists of a series of conditional checks, with any failed check
|
||||
* aborting the request. Nested if/else statements are a standard strategy, but this solution leads
|
||||
* to code fragility and a copy-and-paste style of programming, because the flow of the filtering
|
||||
* and the action of the filters is compiled into the application.
|
||||
*
|
||||
* <p>The key to solving this problem in a flexible and unobtrusive manner is to have a simple
|
||||
* mechanism for adding and removing processing components, in which each component completes a
|
||||
* specific filtering action. This is the Intercepting Filter pattern in action.
|
||||
* <p>
|
||||
* In this example we check whether the order request is valid through pre-processing done via
|
||||
* {@link Filter}. Each field has its own corresponding {@link Filter}
|
||||
* <p>
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
* <p>In this example we check whether the order request is valid through pre-processing done via
|
||||
* {@link Filter}. Each field has its own corresponding {@link Filter}.
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -23,6 +23,9 @@
|
||||
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.GridLayout;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
@ -32,18 +35,15 @@ import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.WindowConstants;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.GridLayout;
|
||||
|
||||
/**
|
||||
* The Client class is responsible for handling the input and running them through filters inside the
|
||||
* {@link FilterManager}.
|
||||
* The Client class is responsible for handling the input and running them through filters inside
|
||||
* the {@link FilterManager}.
|
||||
*
|
||||
* <p>This is where {@link Filter}s come to play as the client pre-processes the request before
|
||||
* being displayed in the {@link Target}.
|
||||
*
|
||||
* This is where {@link Filter}s come to play as the client pre-processes the request before being displayed in the
|
||||
* {@link Target}.
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
*/
|
||||
public class Client extends JFrame { // NOSONAR
|
||||
|
||||
@ -57,7 +57,7 @@ public class Client extends JFrame { // NOSONAR
|
||||
private JButton processButton;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public Client() {
|
||||
super("Client System");
|
||||
@ -107,8 +107,10 @@ public class Client extends JFrame { // NOSONAR
|
||||
});
|
||||
|
||||
processButton.addActionListener(e -> {
|
||||
Order order = new Order(jtFields[0].getText(), jtFields[1].getText(), jtAreas[0].getText(), jtFields[2].getText(),
|
||||
jtAreas[1].getText());
|
||||
Order order =
|
||||
new Order(jtFields[0].getText(), jtFields[1].getText(), jtAreas[0].getText(), jtFields[2]
|
||||
.getText(),
|
||||
jtAreas[1].getText());
|
||||
jl.setText(sendRequest(order));
|
||||
});
|
||||
|
||||
|
@ -26,10 +26,9 @@ package com.iluwatar.intercepting.filter;
|
||||
/**
|
||||
* Concrete implementation of filter This filter checks for the contact field in which it checks if
|
||||
* the input consist of numbers and it also checks if the input follows the length constraint (11
|
||||
* digits)
|
||||
*
|
||||
* @author joshzambales
|
||||
* digits).
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public class ContactFilter extends AbstractFilter {
|
||||
|
||||
|
@ -24,10 +24,9 @@
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Concrete implementation of filter This checks for the deposit code
|
||||
*
|
||||
* @author joshzambales
|
||||
* Concrete implementation of filter This checks for the deposit code.
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public class DepositFilter extends AbstractFilter {
|
||||
|
||||
|
@ -26,9 +26,8 @@ package com.iluwatar.intercepting.filter;
|
||||
/**
|
||||
* Filters perform certain tasks prior or after execution of request by request handler. In this
|
||||
* case, before the request is handled by the target, the request undergoes through each Filter
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public interface Filter {
|
||||
|
||||
|
@ -26,7 +26,7 @@ package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Filter Chain carries multiple filters and help to execute them in defined order on target.
|
||||
*
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public class FilterChain {
|
||||
@ -35,7 +35,7 @@ public class FilterChain {
|
||||
|
||||
|
||||
/**
|
||||
* Adds filter
|
||||
* Adds filter.
|
||||
*/
|
||||
public void addFilter(Filter filter) {
|
||||
if (chain == null) {
|
||||
@ -46,7 +46,7 @@ public class FilterChain {
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute filter chain
|
||||
* Execute filter chain.
|
||||
*/
|
||||
public String execute(Order order) {
|
||||
if (chain != null) {
|
||||
|
@ -25,9 +25,8 @@ package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Filter Manager manages the filters and {@link FilterChain}.
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public class FilterManager {
|
||||
|
||||
|
@ -26,9 +26,8 @@ package com.iluwatar.intercepting.filter;
|
||||
/**
|
||||
* Concrete implementation of filter. This filter checks if the input in the Name field is valid.
|
||||
* (alphanumeric)
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public class NameFilter extends AbstractFilter {
|
||||
|
||||
|
@ -25,7 +25,6 @@ package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Order class carries the order data.
|
||||
*
|
||||
*/
|
||||
public class Order {
|
||||
|
||||
@ -35,12 +34,16 @@ public class Order {
|
||||
private String depositNumber;
|
||||
private String orderItem;
|
||||
|
||||
public Order() {}
|
||||
public Order() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public Order(String name, String contactNumber, String address, String depositNumber, String order) {
|
||||
public Order(
|
||||
String name, String contactNumber, String address,
|
||||
String depositNumber, String order
|
||||
) {
|
||||
this.name = name;
|
||||
this.contactNumber = contactNumber;
|
||||
this.address = address;
|
||||
|
@ -25,9 +25,8 @@ package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Concrete implementation of filter. This checks for the order field.
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
* @author joshzambales
|
||||
*/
|
||||
public class OrderFilter extends AbstractFilter {
|
||||
|
||||
|
@ -23,6 +23,11 @@
|
||||
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
@ -32,16 +37,11 @@ import javax.swing.JTable;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.WindowConstants;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
/**
|
||||
* This is where the requests are displayed after being validated by filters.
|
||||
*
|
||||
* @author mjoshzambales
|
||||
*
|
||||
* @author mjoshzambales
|
||||
*/
|
||||
public class Target extends JFrame { //NOSONAR
|
||||
|
||||
@ -52,14 +52,14 @@ public class Target extends JFrame { //NOSONAR
|
||||
private JButton del;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public Target() {
|
||||
super("Order System");
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
setSize(640, 480);
|
||||
dtm =
|
||||
new DefaultTableModel(new Object[] {"Name", "Contact Number", "Address", "Deposit Number",
|
||||
new DefaultTableModel(new Object[]{"Name", "Contact Number", "Address", "Deposit Number",
|
||||
"Order"}, 0);
|
||||
jt = new JTable(dtm);
|
||||
del = new JButton("Delete");
|
||||
@ -85,7 +85,7 @@ public class Target extends JFrame { //NOSONAR
|
||||
}
|
||||
|
||||
public void execute(String[] request) {
|
||||
dtm.addRow(new Object[] {request[0], request[1], request[2], request[3], request[4]});
|
||||
dtm.addRow(new Object[]{request[0], request[1], request[2], request[3], request[4]});
|
||||
}
|
||||
|
||||
class DListener implements ActionListener {
|
||||
|
@ -23,35 +23,30 @@
|
||||
|
||||
package com.iluwatar.interpreter;
|
||||
|
||||
import java.util.Stack;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Interpreter pattern is a design pattern that specifies how to evaluate sentences in a
|
||||
* language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a
|
||||
* specialized computer language. The syntax tree of a sentence in the language is an instance of
|
||||
* the composite pattern and is used to evaluate (interpret) the sentence for a client.
|
||||
* <p>
|
||||
* In this example we use the Interpreter pattern to break sentences into expressions (
|
||||
* {@link Expression}) that can be evaluated and as a whole form the result.
|
||||
*
|
||||
*
|
||||
* <p>In this example we use the Interpreter pattern to break sentences into expressions ({@link
|
||||
* Expression}) that can be evaluated and as a whole form the result.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
*
|
||||
* Program entry point.
|
||||
* <p>
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations This sample uses postfix,
|
||||
* where operator comes after the operands
|
||||
*
|
||||
*
|
||||
* <p>Expressions can be evaluated using prefix, infix or postfix notations This sample uses
|
||||
* postfix, where operator comes after the operands.
|
||||
*
|
||||
* @param args command line args
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String tokenString = "4 3 2 - 1 + *";
|
||||
@ -84,7 +79,7 @@ public class App {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get expression for string
|
||||
* Get expression for string.
|
||||
*/
|
||||
public static Expression getOperatorInstance(String s, Expression left, Expression right) {
|
||||
switch (s) {
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.interpreter;
|
||||
|
||||
/**
|
||||
*
|
||||
* Expression
|
||||
*
|
||||
* Expression.
|
||||
*/
|
||||
public abstract class Expression {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.interpreter;
|
||||
|
||||
/**
|
||||
*
|
||||
* MinusExpression
|
||||
*
|
||||
* MinusExpression.
|
||||
*/
|
||||
public class MinusExpression extends Expression {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.interpreter;
|
||||
|
||||
/**
|
||||
*
|
||||
* MultiplyExpression
|
||||
*
|
||||
* MultiplyExpression.
|
||||
*/
|
||||
public class MultiplyExpression extends Expression {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.interpreter;
|
||||
|
||||
/**
|
||||
*
|
||||
* NumberExpression
|
||||
*
|
||||
* NumberExpression.
|
||||
*/
|
||||
public class NumberExpression extends Expression {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.interpreter;
|
||||
|
||||
/**
|
||||
*
|
||||
* PlusExpression
|
||||
*
|
||||
* PlusExpression.
|
||||
*/
|
||||
public class PlusExpression extends Expression {
|
||||
|
||||
|
@ -39,8 +39,8 @@ import org.slf4j.LoggerFactory;
|
||||
/**
|
||||
* The Iterator pattern is a design pattern in which an iterator is used to traverse a container and
|
||||
* access the container's elements. The Iterator pattern decouples algorithms from containers.
|
||||
* <p>
|
||||
* In this example the Iterator ({@link Iterator}) adds abstraction layer on top of a collection
|
||||
*
|
||||
* <p>In this example the Iterator ({@link Iterator}) adds abstraction layer on top of a collection
|
||||
* ({@link TreasureChest}). This way the collection can change its internal implementation without
|
||||
* affecting its clients.
|
||||
*/
|
||||
@ -85,7 +85,7 @@ public class App {
|
||||
}
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
|
@ -24,7 +24,8 @@
|
||||
package com.iluwatar.iterator;
|
||||
|
||||
/**
|
||||
* Iterator interface to be implemented by iterators over various data structures
|
||||
* Iterator interface to be implemented by iterators over various data structures.
|
||||
*
|
||||
* @param <T> generically typed for various objects
|
||||
*/
|
||||
public interface Iterator<T> {
|
||||
|
@ -32,7 +32,7 @@ import java.util.NoSuchElementException;
|
||||
* expect to retrieve TreeNodes according to the Integer's natural ordering (1, 2, 3...)
|
||||
*
|
||||
* @param <T> This Iterator has been implemented with generic typing to allow for TreeNodes of
|
||||
* different value types
|
||||
* different value types
|
||||
*/
|
||||
public class BstIterator<T extends Comparable<T>> implements Iterator<TreeNode<T>> {
|
||||
|
||||
@ -46,7 +46,7 @@ public class BstIterator<T extends Comparable<T>> implements Iterator<TreeNode<T
|
||||
/**
|
||||
* This BstIterator manages to use O(h) extra space, where h is the height of the tree It achieves
|
||||
* this by maintaining a stack of the nodes to handle (pushing all left nodes first), before
|
||||
* handling self or right node
|
||||
* handling self or right node.
|
||||
*
|
||||
* @param node TreeNode that acts as root of the subtree we're interested in.
|
||||
*/
|
||||
@ -58,6 +58,8 @@ public class BstIterator<T extends Comparable<T>> implements Iterator<TreeNode<T
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there exists next element.
|
||||
*
|
||||
* @return true if this iterator has a "next" element
|
||||
*/
|
||||
@Override
|
||||
@ -66,6 +68,8 @@ public class BstIterator<T extends Comparable<T>> implements Iterator<TreeNode<T
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next element.
|
||||
*
|
||||
* @return TreeNode next. The next element according to our in-order traversal of the given BST
|
||||
* @throws NoSuchElementException if this iterator does not have a next element
|
||||
*/
|
||||
|
@ -36,7 +36,7 @@ public class TreeNode<T extends Comparable<T>> {
|
||||
private TreeNode<T> right;
|
||||
|
||||
/**
|
||||
* Creates a TreeNode with a given value, and null children
|
||||
* Creates a TreeNode with a given value, and null children.
|
||||
*
|
||||
* @param val The value of the given node
|
||||
*/
|
||||
@ -67,7 +67,7 @@ public class TreeNode<T extends Comparable<T>> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts new TreeNode based on a given value into the subtree represented by self
|
||||
* Inserts new TreeNode based on a given value into the subtree represented by self.
|
||||
*
|
||||
* @param valToInsert The value to insert as a new TreeNode
|
||||
*/
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.iterator.list;
|
||||
|
||||
/**
|
||||
*
|
||||
* Item
|
||||
*
|
||||
* Item.
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.iterator.list;
|
||||
|
||||
/**
|
||||
*
|
||||
* ItemType enumeration
|
||||
*
|
||||
* ItemType enumeration.
|
||||
*/
|
||||
public enum ItemType {
|
||||
|
||||
|
@ -28,29 +28,27 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* TreasureChest, the collection class.
|
||||
*
|
||||
*/
|
||||
public class TreasureChest {
|
||||
|
||||
private List<Item> items;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public TreasureChest() {
|
||||
items = List.of(
|
||||
new Item(ItemType.POTION, "Potion of courage"),
|
||||
new Item(ItemType.RING, "Ring of shadows"),
|
||||
new Item(ItemType.POTION, "Potion of wisdom"),
|
||||
new Item(ItemType.POTION, "Potion of blood"),
|
||||
new Item(ItemType.WEAPON, "Sword of silver +1"),
|
||||
new Item(ItemType.POTION, "Potion of rust"),
|
||||
new Item(ItemType.POTION, "Potion of healing"),
|
||||
new Item(ItemType.RING, "Ring of armor"),
|
||||
new Item(ItemType.WEAPON, "Steel halberd"),
|
||||
new Item(ItemType.WEAPON, "Dagger of poison"));
|
||||
new Item(ItemType.POTION, "Potion of courage"),
|
||||
new Item(ItemType.RING, "Ring of shadows"),
|
||||
new Item(ItemType.POTION, "Potion of wisdom"),
|
||||
new Item(ItemType.POTION, "Potion of blood"),
|
||||
new Item(ItemType.WEAPON, "Sword of silver +1"),
|
||||
new Item(ItemType.POTION, "Potion of rust"),
|
||||
new Item(ItemType.POTION, "Potion of healing"),
|
||||
new Item(ItemType.RING, "Ring of armor"),
|
||||
new Item(ItemType.WEAPON, "Steel halberd"),
|
||||
new Item(ItemType.WEAPON, "Dagger of poison"));
|
||||
}
|
||||
|
||||
public Iterator<Item> iterator(ItemType itemType) {
|
||||
@ -58,7 +56,7 @@ public class TreasureChest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all items
|
||||
* Get all items.
|
||||
*/
|
||||
public List<Item> getItems() {
|
||||
return new ArrayList<>(items);
|
||||
|
@ -27,9 +27,7 @@ import com.iluwatar.iterator.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* TreasureChestItemIterator
|
||||
*
|
||||
* TreasureChestItemIterator.
|
||||
*/
|
||||
public class TreasureChestItemIterator implements Iterator<Item> {
|
||||
|
||||
@ -38,7 +36,7 @@ public class TreasureChestItemIterator implements Iterator<Item> {
|
||||
private ItemType type;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
|
||||
this.chest = chest;
|
||||
|
Reference in New Issue
Block a user