Reformat rest of the design patterns - Issue #224
This commit is contained in:
		@@ -1,46 +1,44 @@
 | 
			
		||||
package com.iluwatar.intercepting.filter;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Base class for order processing filters.
 | 
			
		||||
 * Handles chain management.
 | 
			
		||||
 * Base class for order processing filters. Handles chain management.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public abstract class AbstractFilter implements Filter {
 | 
			
		||||
 | 
			
		||||
	private Filter next;
 | 
			
		||||
	
 | 
			
		||||
	public AbstractFilter() {
 | 
			
		||||
	}
 | 
			
		||||
  private Filter next;
 | 
			
		||||
 | 
			
		||||
	public AbstractFilter(Filter next) {
 | 
			
		||||
		this.next = next;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public void setNext(Filter filter) {
 | 
			
		||||
		this.next = filter;	
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public Filter getNext() {
 | 
			
		||||
		return next;
 | 
			
		||||
	}
 | 
			
		||||
  public AbstractFilter() {}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Filter getLast() {
 | 
			
		||||
		Filter last = this;
 | 
			
		||||
		while (last.getNext() != null) {
 | 
			
		||||
			last = last.getNext();
 | 
			
		||||
		}
 | 
			
		||||
		return last;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public String execute(Order order) {
 | 
			
		||||
		if (getNext() != null) {
 | 
			
		||||
			return getNext().execute(order);
 | 
			
		||||
		} else {
 | 
			
		||||
			return "";
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
  public AbstractFilter(Filter next) {
 | 
			
		||||
    this.next = next;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public void setNext(Filter filter) {
 | 
			
		||||
    this.next = filter;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public Filter getNext() {
 | 
			
		||||
    return next;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public Filter getLast() {
 | 
			
		||||
    Filter last = this;
 | 
			
		||||
    while (last.getNext() != null) {
 | 
			
		||||
      last = last.getNext();
 | 
			
		||||
    }
 | 
			
		||||
    return last;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String execute(Order order) {
 | 
			
		||||
    if (getNext() != null) {
 | 
			
		||||
      return getNext().execute(order);
 | 
			
		||||
    } else {
 | 
			
		||||
      return "";
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,19 +1,20 @@
 | 
			
		||||
package com.iluwatar.intercepting.filter;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Concrete implementation of filter
 | 
			
		||||
 * This filter is responsible for checking/filtering the input in the address field.
 | 
			
		||||
 * Concrete implementation of filter This filter is responsible for checking/filtering the input in
 | 
			
		||||
 * the address field.
 | 
			
		||||
 * 
 | 
			
		||||
 * @author joshzambales
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class AddressFilter extends AbstractFilter {
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public String execute(Order order) {
 | 
			
		||||
		String result = super.execute(order);
 | 
			
		||||
		if (order.getAddress() == null || order.getAddress().isEmpty()) {
 | 
			
		||||
			return result + "Invalid address! ";
 | 
			
		||||
		} else
 | 
			
		||||
			return result;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String execute(Order order) {
 | 
			
		||||
    String result = super.execute(order);
 | 
			
		||||
    if (order.getAddress() == null || order.getAddress().isEmpty()) {
 | 
			
		||||
      return result + "Invalid address! ";
 | 
			
		||||
    } else
 | 
			
		||||
      return result;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,50 +2,46 @@ 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 
 | 
			
		||||
 * 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.
 | 
			
		||||
 * 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.
 | 
			
		||||
 * 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}
 | 
			
		||||
 * 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
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class App{
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Program entry point
 | 
			
		||||
	 * @param args command line args
 | 
			
		||||
	 */
 | 
			
		||||
	public static void main(String[] args) {
 | 
			
		||||
		FilterManager filterManager = new FilterManager(new Target());
 | 
			
		||||
		filterManager.addFilter(new NameFilter());
 | 
			
		||||
		filterManager.addFilter(new ContactFilter());
 | 
			
		||||
		filterManager.addFilter(new AddressFilter());
 | 
			
		||||
		filterManager.addFilter(new DepositFilter());
 | 
			
		||||
		filterManager.addFilter(new OrderFilter());
 | 
			
		||||
public class App {
 | 
			
		||||
 | 
			
		||||
		Client client = new Client();
 | 
			
		||||
		client.setFilterManager(filterManager);
 | 
			
		||||
	}
 | 
			
		||||
  /**
 | 
			
		||||
   * Program entry point
 | 
			
		||||
   * 
 | 
			
		||||
   * @param args command line args
 | 
			
		||||
   */
 | 
			
		||||
  public static void main(String[] args) {
 | 
			
		||||
    FilterManager filterManager = new FilterManager(new Target());
 | 
			
		||||
    filterManager.addFilter(new NameFilter());
 | 
			
		||||
    filterManager.addFilter(new ContactFilter());
 | 
			
		||||
    filterManager.addFilter(new AddressFilter());
 | 
			
		||||
    filterManager.addFilter(new DepositFilter());
 | 
			
		||||
    filterManager.addFilter(new OrderFilter());
 | 
			
		||||
 | 
			
		||||
    Client client = new Client();
 | 
			
		||||
    client.setFilterManager(filterManager);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -15,93 +15,95 @@ import javax.swing.JTextField;
 | 
			
		||||
import javax.swing.SwingUtilities;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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}.
 | 
			
		||||
 * 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 {
 | 
			
		||||
 | 
			
		||||
	private static final long serialVersionUID = 1L;
 | 
			
		||||
  private static final long serialVersionUID = 1L;
 | 
			
		||||
 | 
			
		||||
	private FilterManager filterManager;
 | 
			
		||||
	private JLabel jl;
 | 
			
		||||
	private JTextField[] jtFields;
 | 
			
		||||
	private JTextArea[] jtAreas;
 | 
			
		||||
	private JButton clearButton, processButton;
 | 
			
		||||
  private FilterManager filterManager;
 | 
			
		||||
  private JLabel jl;
 | 
			
		||||
  private JTextField[] jtFields;
 | 
			
		||||
  private JTextArea[] jtAreas;
 | 
			
		||||
  private JButton clearButton, processButton;
 | 
			
		||||
 | 
			
		||||
	public Client() {
 | 
			
		||||
		super("Client System");
 | 
			
		||||
		setDefaultCloseOperation(EXIT_ON_CLOSE);
 | 
			
		||||
		setSize(300, 300);
 | 
			
		||||
		jl = new JLabel("RUNNING...");
 | 
			
		||||
		jtFields = new JTextField[3];
 | 
			
		||||
		for (int i = 0; i < 3; i++) {
 | 
			
		||||
			jtFields[i] = new JTextField();
 | 
			
		||||
		}
 | 
			
		||||
		jtAreas = new JTextArea[2];
 | 
			
		||||
		for (int i = 0; i < 2; i++) {
 | 
			
		||||
			jtAreas[i] = new JTextArea();
 | 
			
		||||
		}
 | 
			
		||||
		clearButton = new JButton("Clear");
 | 
			
		||||
		processButton = new JButton("Process");
 | 
			
		||||
  public Client() {
 | 
			
		||||
    super("Client System");
 | 
			
		||||
    setDefaultCloseOperation(EXIT_ON_CLOSE);
 | 
			
		||||
    setSize(300, 300);
 | 
			
		||||
    jl = new JLabel("RUNNING...");
 | 
			
		||||
    jtFields = new JTextField[3];
 | 
			
		||||
    for (int i = 0; i < 3; i++) {
 | 
			
		||||
      jtFields[i] = new JTextField();
 | 
			
		||||
    }
 | 
			
		||||
    jtAreas = new JTextArea[2];
 | 
			
		||||
    for (int i = 0; i < 2; i++) {
 | 
			
		||||
      jtAreas[i] = new JTextArea();
 | 
			
		||||
    }
 | 
			
		||||
    clearButton = new JButton("Clear");
 | 
			
		||||
    processButton = new JButton("Process");
 | 
			
		||||
 | 
			
		||||
		setup();
 | 
			
		||||
	}
 | 
			
		||||
    setup();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	private void setup() {
 | 
			
		||||
		setLayout(new BorderLayout());
 | 
			
		||||
		JPanel panel = new JPanel();
 | 
			
		||||
		add(jl, BorderLayout.SOUTH);
 | 
			
		||||
		add(panel, BorderLayout.CENTER);
 | 
			
		||||
		panel.setLayout(new GridLayout(6, 2));
 | 
			
		||||
		panel.add(new JLabel("Name"));
 | 
			
		||||
		panel.add(jtFields[0]);
 | 
			
		||||
		panel.add(new JLabel("Contact Number"));
 | 
			
		||||
		panel.add(jtFields[1]);
 | 
			
		||||
		panel.add(new JLabel("Address"));
 | 
			
		||||
		panel.add(jtAreas[0]);
 | 
			
		||||
		panel.add(new JLabel("Deposit Number"));
 | 
			
		||||
		panel.add(jtFields[2]);
 | 
			
		||||
		panel.add(new JLabel("Order"));
 | 
			
		||||
		panel.add(jtAreas[1]);
 | 
			
		||||
		panel.add(clearButton);
 | 
			
		||||
		panel.add(processButton);
 | 
			
		||||
  private void setup() {
 | 
			
		||||
    setLayout(new BorderLayout());
 | 
			
		||||
    JPanel panel = new JPanel();
 | 
			
		||||
    add(jl, BorderLayout.SOUTH);
 | 
			
		||||
    add(panel, BorderLayout.CENTER);
 | 
			
		||||
    panel.setLayout(new GridLayout(6, 2));
 | 
			
		||||
    panel.add(new JLabel("Name"));
 | 
			
		||||
    panel.add(jtFields[0]);
 | 
			
		||||
    panel.add(new JLabel("Contact Number"));
 | 
			
		||||
    panel.add(jtFields[1]);
 | 
			
		||||
    panel.add(new JLabel("Address"));
 | 
			
		||||
    panel.add(jtAreas[0]);
 | 
			
		||||
    panel.add(new JLabel("Deposit Number"));
 | 
			
		||||
    panel.add(jtFields[2]);
 | 
			
		||||
    panel.add(new JLabel("Order"));
 | 
			
		||||
    panel.add(jtAreas[1]);
 | 
			
		||||
    panel.add(clearButton);
 | 
			
		||||
    panel.add(processButton);
 | 
			
		||||
 | 
			
		||||
		clearButton.addActionListener(new ActionListener() {
 | 
			
		||||
			@Override
 | 
			
		||||
			public void actionPerformed(ActionEvent e) {
 | 
			
		||||
				for (JTextArea i : jtAreas) {
 | 
			
		||||
					i.setText("");
 | 
			
		||||
				}
 | 
			
		||||
				for (JTextField i : jtFields) {
 | 
			
		||||
					i.setText("");
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		});
 | 
			
		||||
    clearButton.addActionListener(new ActionListener() {
 | 
			
		||||
      @Override
 | 
			
		||||
      public void actionPerformed(ActionEvent e) {
 | 
			
		||||
        for (JTextArea i : jtAreas) {
 | 
			
		||||
          i.setText("");
 | 
			
		||||
        }
 | 
			
		||||
        for (JTextField i : jtFields) {
 | 
			
		||||
          i.setText("");
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
		processButton.addActionListener(new ActionListener() {
 | 
			
		||||
			@Override
 | 
			
		||||
			public void actionPerformed(ActionEvent e) {
 | 
			
		||||
				Order order = new Order(jtFields[0].getText(), jtFields[1]
 | 
			
		||||
						.getText(), jtAreas[0].getText(),
 | 
			
		||||
						jtFields[2].getText(), jtAreas[1].getText());
 | 
			
		||||
				jl.setText(sendRequest(order));
 | 
			
		||||
			}
 | 
			
		||||
		});
 | 
			
		||||
    processButton.addActionListener(new ActionListener() {
 | 
			
		||||
      @Override
 | 
			
		||||
      public void actionPerformed(ActionEvent e) {
 | 
			
		||||
        Order order =
 | 
			
		||||
            new Order(jtFields[0].getText(), jtFields[1].getText(), jtAreas[0].getText(),
 | 
			
		||||
                jtFields[2].getText(), jtAreas[1].getText());
 | 
			
		||||
        jl.setText(sendRequest(order));
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
		JRootPane rootPane = SwingUtilities.getRootPane(processButton);
 | 
			
		||||
		rootPane.setDefaultButton(processButton);
 | 
			
		||||
		setVisible(true);
 | 
			
		||||
	}
 | 
			
		||||
    JRootPane rootPane = SwingUtilities.getRootPane(processButton);
 | 
			
		||||
    rootPane.setDefaultButton(processButton);
 | 
			
		||||
    setVisible(true);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public void setFilterManager(FilterManager filterManager) {
 | 
			
		||||
		this.filterManager = filterManager;
 | 
			
		||||
	}
 | 
			
		||||
  public void setFilterManager(FilterManager filterManager) {
 | 
			
		||||
    this.filterManager = filterManager;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public String sendRequest(Order order) {
 | 
			
		||||
		return filterManager.filterRequest(order);
 | 
			
		||||
	}
 | 
			
		||||
  public String sendRequest(Order order) {
 | 
			
		||||
    return filterManager.filterRequest(order);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,24 +1,24 @@
 | 
			
		||||
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)
 | 
			
		||||
 * 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
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class ContactFilter extends AbstractFilter {
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public String execute(Order order) {
 | 
			
		||||
		String result = super.execute(order);
 | 
			
		||||
		if (order.getContactNumber() == null
 | 
			
		||||
				|| order.getContactNumber().isEmpty()
 | 
			
		||||
				|| order.getContactNumber().matches(".*[^\\d]+.*")
 | 
			
		||||
				|| order.getContactNumber().length() != 11) {
 | 
			
		||||
			return result + "Invalid contact number! ";
 | 
			
		||||
		} else {
 | 
			
		||||
			return result;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String execute(Order order) {
 | 
			
		||||
    String result = super.execute(order);
 | 
			
		||||
    if (order.getContactNumber() == null || order.getContactNumber().isEmpty()
 | 
			
		||||
        || order.getContactNumber().matches(".*[^\\d]+.*")
 | 
			
		||||
        || order.getContactNumber().length() != 11) {
 | 
			
		||||
      return result + "Invalid contact number! ";
 | 
			
		||||
    } else {
 | 
			
		||||
      return result;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,20 @@
 | 
			
		||||
package com.iluwatar.intercepting.filter;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Concrete implementation of filter
 | 
			
		||||
 * This checks for the deposit code 
 | 
			
		||||
 * Concrete implementation of filter This checks for the deposit code
 | 
			
		||||
 * 
 | 
			
		||||
 * @author joshzambales
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class DepositFilter extends AbstractFilter {
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public String execute(Order order) {
 | 
			
		||||
		String result = super.execute(order);
 | 
			
		||||
		if (order.getDepositNumber() == null || order.getDepositNumber().isEmpty()) {
 | 
			
		||||
			return result + "Invalid deposit number! ";
 | 
			
		||||
		} else {
 | 
			
		||||
			return result;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String execute(Order order) {
 | 
			
		||||
    String result = super.execute(order);
 | 
			
		||||
    if (order.getDepositNumber() == null || order.getDepositNumber().isEmpty()) {
 | 
			
		||||
      return result + "Invalid deposit number! ";
 | 
			
		||||
    } else {
 | 
			
		||||
      return result;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,37 +1,40 @@
 | 
			
		||||
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
 | 
			
		||||
 * 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
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public interface Filter {
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Execute order processing filter.
 | 
			
		||||
	 * @param order
 | 
			
		||||
	 * @return empty string on success, otherwise error message.
 | 
			
		||||
	 */
 | 
			
		||||
	String execute(Order order);
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Set next filter in chain after this.
 | 
			
		||||
	 * @param filter
 | 
			
		||||
	 */
 | 
			
		||||
	void setNext(Filter filter);
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get next filter in chain after this.
 | 
			
		||||
	 * @return
 | 
			
		||||
	 */
 | 
			
		||||
	Filter getNext();
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get last filter in the chain.
 | 
			
		||||
	 * @return
 | 
			
		||||
	 */
 | 
			
		||||
	Filter getLast();
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Execute order processing filter.
 | 
			
		||||
   * 
 | 
			
		||||
   * @param order
 | 
			
		||||
   * @return empty string on success, otherwise error message.
 | 
			
		||||
   */
 | 
			
		||||
  String execute(Order order);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Set next filter in chain after this.
 | 
			
		||||
   * 
 | 
			
		||||
   * @param filter
 | 
			
		||||
   */
 | 
			
		||||
  void setNext(Filter filter);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get next filter in chain after this.
 | 
			
		||||
   * 
 | 
			
		||||
   * @return
 | 
			
		||||
   */
 | 
			
		||||
  Filter getNext();
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get last filter in the chain.
 | 
			
		||||
   * 
 | 
			
		||||
   * @return
 | 
			
		||||
   */
 | 
			
		||||
  Filter getLast();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,34 +1,34 @@
 | 
			
		||||
 package com.iluwatar.intercepting.filter;
 | 
			
		||||
 
 | 
			
		||||
  
 | 
			
		||||
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 {
 | 
			
		||||
	
 | 
			
		||||
	private Filter chain;
 | 
			
		||||
	
 | 
			
		||||
	private final Target target;
 | 
			
		||||
 | 
			
		||||
	public FilterChain(Target target) {
 | 
			
		||||
		this.target = target;
 | 
			
		||||
	}
 | 
			
		||||
  private Filter chain;
 | 
			
		||||
 | 
			
		||||
	public void addFilter(Filter filter) {
 | 
			
		||||
		if (chain == null) {
 | 
			
		||||
			chain = filter;
 | 
			
		||||
		} else {
 | 
			
		||||
			chain.getLast().setNext(filter);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
  private final Target target;
 | 
			
		||||
 | 
			
		||||
	public String execute(Order order) {
 | 
			
		||||
		if (chain != null) {
 | 
			
		||||
			return chain.execute(order);
 | 
			
		||||
		} else {
 | 
			
		||||
			return "RUNNING...";
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
  public FilterChain(Target target) {
 | 
			
		||||
    this.target = target;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void addFilter(Filter filter) {
 | 
			
		||||
    if (chain == null) {
 | 
			
		||||
      chain = filter;
 | 
			
		||||
    } else {
 | 
			
		||||
      chain.getLast().setNext(filter);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String execute(Order order) {
 | 
			
		||||
    if (chain != null) {
 | 
			
		||||
      return chain.execute(order);
 | 
			
		||||
    } else {
 | 
			
		||||
      return "RUNNING...";
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,18 +7,18 @@ package com.iluwatar.intercepting.filter;
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class FilterManager {
 | 
			
		||||
	
 | 
			
		||||
	private FilterChain filterChain;
 | 
			
		||||
 | 
			
		||||
	public FilterManager(Target target) {
 | 
			
		||||
		filterChain = new FilterChain(target);
 | 
			
		||||
	}
 | 
			
		||||
  private FilterChain filterChain;
 | 
			
		||||
 | 
			
		||||
	public void addFilter(Filter filter) {
 | 
			
		||||
		filterChain.addFilter(filter);
 | 
			
		||||
	}
 | 
			
		||||
  public FilterManager(Target target) {
 | 
			
		||||
    filterChain = new FilterChain(target);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public String filterRequest(Order order) {
 | 
			
		||||
		return filterChain.execute(order);
 | 
			
		||||
	}
 | 
			
		||||
  public void addFilter(Filter filter) {
 | 
			
		||||
    filterChain.addFilter(filter);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String filterRequest(Order order) {
 | 
			
		||||
    return filterChain.execute(order);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,21 +1,22 @@
 | 
			
		||||
package com.iluwatar.intercepting.filter;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Concrete implementation of filter. This filter checks if the input in the Name
 | 
			
		||||
 * field is valid. (alphanumeric)
 | 
			
		||||
 * Concrete implementation of filter. This filter checks if the input in the Name field is valid.
 | 
			
		||||
 * (alphanumeric)
 | 
			
		||||
 * 
 | 
			
		||||
 * @author joshzambales
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class NameFilter extends AbstractFilter {
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public String execute(Order order) {
 | 
			
		||||
		String result = super.execute(order);
 | 
			
		||||
		if (order.getName() == null || order.getName().isEmpty() || order.getName().matches(".*[^\\w|\\s]+.*")) {
 | 
			
		||||
			return result + "Invalid order! ";
 | 
			
		||||
		} else {
 | 
			
		||||
			return result;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String execute(Order order) {
 | 
			
		||||
    String result = super.execute(order);
 | 
			
		||||
    if (order.getName() == null || order.getName().isEmpty()
 | 
			
		||||
        || order.getName().matches(".*[^\\w|\\s]+.*")) {
 | 
			
		||||
      return result + "Invalid order! ";
 | 
			
		||||
    } else {
 | 
			
		||||
      return result;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,60 +6,59 @@ package com.iluwatar.intercepting.filter;
 | 
			
		||||
 */
 | 
			
		||||
public class Order {
 | 
			
		||||
 | 
			
		||||
	private String name;
 | 
			
		||||
	private String contactNumber;
 | 
			
		||||
	private String address;
 | 
			
		||||
	private String depositNumber;
 | 
			
		||||
	private String order;
 | 
			
		||||
	
 | 
			
		||||
	public Order() {
 | 
			
		||||
	}
 | 
			
		||||
  private String name;
 | 
			
		||||
  private String contactNumber;
 | 
			
		||||
  private String address;
 | 
			
		||||
  private String depositNumber;
 | 
			
		||||
  private String order;
 | 
			
		||||
 | 
			
		||||
	public Order(String name, String contactNumber, String address, String depositNumber, String order) {
 | 
			
		||||
		this.name = name;
 | 
			
		||||
		this.contactNumber = contactNumber;
 | 
			
		||||
		this.address = address;
 | 
			
		||||
		this.depositNumber = depositNumber;
 | 
			
		||||
		this.order = order;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public String getName() {
 | 
			
		||||
		return name;
 | 
			
		||||
	}
 | 
			
		||||
  public Order() {}
 | 
			
		||||
 | 
			
		||||
	public void setName(String name) {
 | 
			
		||||
		this.name = name;
 | 
			
		||||
	}
 | 
			
		||||
  public Order(String name, String contactNumber, String address, String depositNumber, String order) {
 | 
			
		||||
    this.name = name;
 | 
			
		||||
    this.contactNumber = contactNumber;
 | 
			
		||||
    this.address = address;
 | 
			
		||||
    this.depositNumber = depositNumber;
 | 
			
		||||
    this.order = order;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public String getContactNumber() {
 | 
			
		||||
		return contactNumber;
 | 
			
		||||
	}
 | 
			
		||||
  public String getName() {
 | 
			
		||||
    return name;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public void setContactNumber(String contactNumber) {
 | 
			
		||||
		this.contactNumber = contactNumber;
 | 
			
		||||
	}
 | 
			
		||||
  public void setName(String name) {
 | 
			
		||||
    this.name = name;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public String getAddress() {
 | 
			
		||||
		return address;
 | 
			
		||||
	}
 | 
			
		||||
  public String getContactNumber() {
 | 
			
		||||
    return contactNumber;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public void setAddress(String address) {
 | 
			
		||||
		this.address = address;
 | 
			
		||||
	}
 | 
			
		||||
  public void setContactNumber(String contactNumber) {
 | 
			
		||||
    this.contactNumber = contactNumber;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public String getDepositNumber() {
 | 
			
		||||
		return depositNumber;
 | 
			
		||||
	}
 | 
			
		||||
  public String getAddress() {
 | 
			
		||||
    return address;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public void setDepositNumber(String depositNumber) {
 | 
			
		||||
		this.depositNumber = depositNumber;
 | 
			
		||||
	}
 | 
			
		||||
  public void setAddress(String address) {
 | 
			
		||||
    this.address = address;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public String getOrder() {
 | 
			
		||||
		return order;
 | 
			
		||||
	}
 | 
			
		||||
  public String getDepositNumber() {
 | 
			
		||||
    return depositNumber;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public void setOrder(String order) {
 | 
			
		||||
		this.order = order;
 | 
			
		||||
	}
 | 
			
		||||
  public void setDepositNumber(String depositNumber) {
 | 
			
		||||
    this.depositNumber = depositNumber;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getOrder() {
 | 
			
		||||
    return order;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setOrder(String order) {
 | 
			
		||||
    this.order = order;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,14 +7,14 @@ package com.iluwatar.intercepting.filter;
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class OrderFilter extends AbstractFilter {
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public String execute(Order order) {
 | 
			
		||||
		String result = super.execute(order);
 | 
			
		||||
		if (order.getOrder() == null || order.getOrder().isEmpty()) {
 | 
			
		||||
			return result + "Invalid order! ";
 | 
			
		||||
		} else {
 | 
			
		||||
			return result;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String execute(Order order) {
 | 
			
		||||
    String result = super.execute(order);
 | 
			
		||||
    if (order.getOrder() == null || order.getOrder().isEmpty()) {
 | 
			
		||||
      return result + "Invalid order! ";
 | 
			
		||||
    } else {
 | 
			
		||||
      return result;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -22,57 +22,57 @@ import javax.swing.table.DefaultTableModel;
 | 
			
		||||
 */
 | 
			
		||||
public class Target extends JFrame {
 | 
			
		||||
 | 
			
		||||
	private static final long serialVersionUID = 1L;
 | 
			
		||||
  private static final long serialVersionUID = 1L;
 | 
			
		||||
 | 
			
		||||
	private JTable jt;
 | 
			
		||||
	private JScrollPane jsp;
 | 
			
		||||
	private DefaultTableModel dtm;
 | 
			
		||||
	private JButton del;
 | 
			
		||||
  private JTable jt;
 | 
			
		||||
  private JScrollPane jsp;
 | 
			
		||||
  private DefaultTableModel dtm;
 | 
			
		||||
  private JButton del;
 | 
			
		||||
 | 
			
		||||
	public Target() {
 | 
			
		||||
		super("Order System");
 | 
			
		||||
		setDefaultCloseOperation(EXIT_ON_CLOSE);
 | 
			
		||||
		setSize(640, 480);
 | 
			
		||||
		dtm = new DefaultTableModel(new Object[] { "Name", "Contact Number",
 | 
			
		||||
				"Address", "Deposit Number", "Order" }, 0);
 | 
			
		||||
		jt = new JTable(dtm);
 | 
			
		||||
		del = new JButton("Delete");
 | 
			
		||||
		setup();
 | 
			
		||||
	}
 | 
			
		||||
  public Target() {
 | 
			
		||||
    super("Order System");
 | 
			
		||||
    setDefaultCloseOperation(EXIT_ON_CLOSE);
 | 
			
		||||
    setSize(640, 480);
 | 
			
		||||
    dtm =
 | 
			
		||||
        new DefaultTableModel(new Object[] {"Name", "Contact Number", "Address", "Deposit Number",
 | 
			
		||||
            "Order"}, 0);
 | 
			
		||||
    jt = new JTable(dtm);
 | 
			
		||||
    del = new JButton("Delete");
 | 
			
		||||
    setup();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	private void setup() {
 | 
			
		||||
		setLayout(new BorderLayout());
 | 
			
		||||
		JPanel bot = new JPanel();
 | 
			
		||||
		add(jt.getTableHeader(), BorderLayout.NORTH);
 | 
			
		||||
		bot.setLayout(new BorderLayout());
 | 
			
		||||
		bot.add(del, BorderLayout.EAST);
 | 
			
		||||
		add(bot, BorderLayout.SOUTH);
 | 
			
		||||
		jsp = new JScrollPane(jt);
 | 
			
		||||
		jsp.setPreferredSize(new Dimension(500, 250));
 | 
			
		||||
		add(jsp, BorderLayout.CENTER);
 | 
			
		||||
  private void setup() {
 | 
			
		||||
    setLayout(new BorderLayout());
 | 
			
		||||
    JPanel bot = new JPanel();
 | 
			
		||||
    add(jt.getTableHeader(), BorderLayout.NORTH);
 | 
			
		||||
    bot.setLayout(new BorderLayout());
 | 
			
		||||
    bot.add(del, BorderLayout.EAST);
 | 
			
		||||
    add(bot, BorderLayout.SOUTH);
 | 
			
		||||
    jsp = new JScrollPane(jt);
 | 
			
		||||
    jsp.setPreferredSize(new Dimension(500, 250));
 | 
			
		||||
    add(jsp, BorderLayout.CENTER);
 | 
			
		||||
 | 
			
		||||
		del.addActionListener(new DListener());
 | 
			
		||||
    del.addActionListener(new DListener());
 | 
			
		||||
 | 
			
		||||
		JRootPane rootPane = SwingUtilities.getRootPane(del);
 | 
			
		||||
		rootPane.setDefaultButton(del);
 | 
			
		||||
		setVisible(true);
 | 
			
		||||
	}
 | 
			
		||||
    JRootPane rootPane = SwingUtilities.getRootPane(del);
 | 
			
		||||
    rootPane.setDefaultButton(del);
 | 
			
		||||
    setVisible(true);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	public void execute(String[] request) {
 | 
			
		||||
		dtm.addRow(new Object[] { request[0], request[1], request[2],
 | 
			
		||||
				request[3], request[4] });
 | 
			
		||||
	}
 | 
			
		||||
  public void execute(String[] request) {
 | 
			
		||||
    dtm.addRow(new Object[] {request[0], request[1], request[2], request[3], request[4]});
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	class DListener implements ActionListener {
 | 
			
		||||
		@Override
 | 
			
		||||
		public void actionPerformed(ActionEvent e) {
 | 
			
		||||
			int temp = jt.getSelectedRow();
 | 
			
		||||
			if (temp == -1)
 | 
			
		||||
				return;
 | 
			
		||||
			int temp2 = jt.getSelectedRowCount();
 | 
			
		||||
			for (int i = 0; i < temp2; i++) {
 | 
			
		||||
				dtm.removeRow(temp);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
  class DListener implements ActionListener {
 | 
			
		||||
    @Override
 | 
			
		||||
    public void actionPerformed(ActionEvent e) {
 | 
			
		||||
      int temp = jt.getSelectedRow();
 | 
			
		||||
      if (temp == -1)
 | 
			
		||||
        return;
 | 
			
		||||
      int temp2 = jt.getSelectedRowCount();
 | 
			
		||||
      for (int i = 0; i < temp2; i++) {
 | 
			
		||||
        dtm.removeRow(temp);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -11,9 +11,9 @@ import com.iluwatar.intercepting.filter.App;
 | 
			
		||||
 */
 | 
			
		||||
public class AppTest {
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void test() {
 | 
			
		||||
		String[] args = {};
 | 
			
		||||
		App.main(args);
 | 
			
		||||
	}
 | 
			
		||||
  @Test
 | 
			
		||||
  public void test() {
 | 
			
		||||
    String[] args = {};
 | 
			
		||||
    App.main(args);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user