Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

/**
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
/**
* 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,33 @@
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
* violate any constraints? - What encoding does the client use to send the data? - Do we support
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,18 +35,15 @@
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}.
*
* 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
* <p>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

Expand All @@ -57,7 +57,7 @@ public class Client extends JFrame { // NOSONAR
private JButton processButton;

/**
* Constructor
* Constructor.
*/
public Client() {
super("Client System");
Expand Down Expand Up @@ -107,8 +107,10 @@ private void setup() {
});

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));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
/**
* 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
/**
* 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/**
* Filter Chain carries multiple filters and help to execute them in defined order on target.
*
*
* @author joshzambales
*/
public class FilterChain {
Expand All @@ -35,7 +35,7 @@ public class FilterChain {


/**
* Adds filter
* Adds filter.
*/
public void addFilter(Filter filter) {
if (chain == null) {
Expand All @@ -46,7 +46,7 @@ public void addFilter(Filter filter) {
}

/**
* Execute filter chain
* Execute filter chain.
*/
public String execute(Order order) {
if (chain != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

/**
* Filter Manager manages the filters and {@link FilterChain}.
*
* @author joshzambales
*
* @author joshzambales
*/
public class FilterManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
/**
* 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

/**
* Order class carries the order data.
*
*/
public class Order {

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

/**
* Concrete implementation of filter. This checks for the order field.
*
* @author joshzambales
*
* @author joshzambales
*/
public class OrderFilter extends AbstractFilter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,16 +37,11 @@
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

Expand All @@ -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");
Expand All @@ -85,7 +85,7 @@ private void setup() {
}

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 {
Expand Down
23 changes: 9 additions & 14 deletions interpreter/src/main/java/com/iluwatar/interpreter/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 + *";
Expand Down Expand Up @@ -84,7 +79,7 @@ public static boolean isOperator(String s) {
}

/**
* Get expression for string
* Get expression for string.
*/
public static Expression getOperatorInstance(String s, Expression left, Expression right) {
switch (s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.interpreter;

/**
*
* Expression
*
* Expression.
*/
public abstract class Expression {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.interpreter;

/**
*
* MinusExpression
*
* MinusExpression.
*/
public class MinusExpression extends Expression {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.interpreter;

/**
*
* MultiplyExpression
*
* MultiplyExpression.
*/
public class MultiplyExpression extends Expression {

Expand Down
Loading