-
-
Notifications
You must be signed in to change notification settings - Fork 27k
Updated pull request #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7ff8802
97c5c13
f64480d
efb032a
b4aed8f
543c062
4118aab
092e6ef
915145e
5e939a2
24ea7ff
bc98c11
aca7558
2392d6c
f753d68
6d4e473
ddace11
68f02c1
ee37e10
5383eb9
10be0b0
2d7e2d1
57c7a48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.iluwatar</groupId> | ||
<artifactId>java-design-patterns</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>intercepting-filter</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.iluwatar; | ||
/** | ||
* Concrete implementation of filter | ||
* This filter is responsible for checking/filtering the input in the address field, returns null if field is empty | ||
* @author joshzambales | ||
* | ||
*/ | ||
public class AddressFilter implements Filter{ | ||
public String execute(String[] request){ | ||
if(request[2].equals("")){ | ||
return null; | ||
}else return request[2]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.iluwatar; | ||
import java.util.*; | ||
import javax.swing.*; | ||
import javax.swing.table.*; | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
/** | ||
* | ||
* This is an app that checks whether the order request is valid through pre-processing done via Filters | ||
* Each field has its own corresponding Filter | ||
* @author joshzambales | ||
* | ||
*/ | ||
public class App{ | ||
public static void main(String[] args){ | ||
FilterManager filterManager = new FilterManager(new Target()); | ||
filterManager.setFilter(new NameFilter()); | ||
filterManager.setFilter(new ContactFilter()); | ||
filterManager.setFilter(new AddressFilter()); | ||
filterManager.setFilter(new DepositFilter()); | ||
filterManager.setFilter(new OrderFilter()); | ||
|
||
Client client = new Client(); | ||
client.setFilterManager(filterManager); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.iluwatar; | ||
import java.util.*; | ||
import javax.swing.*; | ||
import javax.swing.table.*; | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
|
||
/** | ||
* The Client class is responsible for handling the input and running them through filters inside the filterManager | ||
* | ||
* This is where Filters come to play as the client pre-processes the request before being displayed in the Target | ||
* | ||
* @author joshzambales | ||
* | ||
*/ | ||
public class Client extends JFrame{ | ||
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"); | ||
|
||
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); | ||
|
||
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){ | ||
String request = String.format("%s&%s&%s&%s&%s",jtFields[0].getText(),jtFields[1].getText(),jtAreas[0].getText(),jtFields[2].getText(),jtAreas[1].getText()); | ||
|
||
jl.setText(sendRequest(request)); | ||
} | ||
}); | ||
|
||
JRootPane rootPane = SwingUtilities.getRootPane(processButton); | ||
rootPane.setDefaultButton(processButton); | ||
setVisible(true); | ||
} | ||
public void setFilterManager(FilterManager filterManager){ | ||
this.filterManager = filterManager; | ||
} | ||
public String sendRequest(String request){ | ||
return filterManager.filterRequest(request); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.iluwatar; | ||
/** | ||
* 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 implements Filter{ | ||
public String execute(String[] request){ | ||
if(request[1].equals("") || request[1].matches(".*[^\\d]+.*") || request[1].length() != 11){ | ||
return null; | ||
}else return request[1]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.iluwatar; | ||
/** | ||
* Concrete implementation of filter | ||
* | ||
* This checks for the deposit code, returns null when deposit field is empty | ||
* @author joshzambales | ||
* | ||
*/ | ||
public class DepositFilter implements Filter{ | ||
public String execute(String[] request){ | ||
if(request[3].equals("")){ | ||
return null; | ||
}else return request[3]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.iluwatar; | ||
/** | ||
* Filter interface | ||
* 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{ | ||
public String execute(String[] request); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.iluwatar; | ||
import java.util.*; | ||
/** | ||
* Filter Chain carries multiple filters and help to execute them in defined order on target. | ||
* | ||
* @author joshzambales | ||
*/ | ||
public class FilterChain{ | ||
private ArrayList<Filter> filters = new ArrayList<Filter>(); | ||
private final Target target; | ||
|
||
public FilterChain(Target target){ | ||
this.target = target; | ||
} | ||
public void addFilter(Filter filter){ | ||
filters.add(filter); | ||
} | ||
|
||
public String execute(String request){ | ||
String tempout[] = new String[filters.size()]; | ||
|
||
String tempin[] = request.split("&"); | ||
int i = 0; | ||
try{ | ||
for(Filter filter:filters){ | ||
tempout[i] = null; | ||
tempout[i++] = filter.execute(tempin); | ||
} | ||
}catch(Exception e){ | ||
return "NOT ENOUGHT INPUT"; | ||
} | ||
|
||
if(tempout[4] == null){ | ||
return "INVALID ORDER!"; | ||
}else if(tempout[3] == null){ | ||
return "INVALID DEPOSIT NUMBER!"; | ||
}else if(tempout[2] == null){ | ||
return "INVALID ADRDESS!"; | ||
}else if(tempout[1] == null){ | ||
return "INVALID Contact Number!"; | ||
}else if(tempout[0] == null){ | ||
return "INVALID Name!"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't know if this will violate pattern structure or filters reusability, but I would like to keep related things together. And one step further (although its also can have more problem with pattern consistency): |
||
}else{ | ||
target.execute(tempout); | ||
return "RUNNING..."; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.iluwatar; | ||
import java.util.*; | ||
import javax.swing.*; | ||
import javax.swing.table.*; | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
/** | ||
* Filter Manager manages the filters and Filter Chain. | ||
* @author joshzambales | ||
* | ||
*/ | ||
public class FilterManager{ | ||
private FilterChain filterChain; | ||
|
||
public FilterManager(Target target){ | ||
filterChain = new FilterChain(target); | ||
} | ||
public void setFilter(Filter filter){ | ||
filterChain.addFilter(filter); | ||
} | ||
public String filterRequest(String request){ | ||
return filterChain.execute(request); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.iluwatar; | ||
/** | ||
* Concrete implementation of filter | ||
* This filter checks if the input in the Name field is valid. (alphanumeric) | ||
* @author joshzambales | ||
* | ||
*/ | ||
public class NameFilter implements Filter{ | ||
public String execute(String[] request){ | ||
if(request[0].equals("") || request[0].matches(".*[^\\w|\\s]+.*")){ | ||
return null; | ||
}else return request[0]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.iluwatar; | ||
/** | ||
* Concrete implementation of filter | ||
* This checks for the order field, returns null when order field is empty | ||
* | ||
* @author joshzambales | ||
* | ||
*/ | ||
public class OrderFilter implements Filter{ | ||
public String execute(String[] request){ | ||
if(request[4].equals("")){ | ||
return null; | ||
}else return request[4]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be: Presentation Tier Patterns