Skip to content

Commit

Permalink
Merge pull request #7 from albertopoljak/dev
Browse files Browse the repository at this point in the history
Merge dev with main
  • Loading branch information
albertopoljak committed Nov 24, 2017
2 parents 2c887ff + 315b429 commit b2eb122
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 168 deletions.
131 changes: 131 additions & 0 deletions src/main/InputConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.swing.JTextPane;

public class InputConsole extends JTextPane {
private String newLine = System.getProperty("line.separator");

/*
* Extract inputed words into ArrayList
* Recognizes words based on variable wordSeparator (newline is considered as word separator.)
* Blank words are eliminated
* Duplicates are not eliminated
* String input can't be null
*/
public ArrayList<String> extractInput(String input ,char wordSeparator){
ArrayList<String> returnedWords = new ArrayList<String>();
String tempWord = "";
char currentChar;
int inputLength = input.length();

if (inputLength == 0) {
Main.txtrLog.append("Input is empty!" , 'W');
return null;
}


for (int i=0; i<inputLength; i++) {
currentChar = input.charAt(i);
if (currentChar!=wordSeparator && !compareCharForNewline(currentChar, newLine) )
tempWord+=input.charAt(i);
else{
if (tempWord.length()>0)
returnedWords.add(tempWord);
else
Main.txtrLog.appendDebug("Skipping empty word (you probably typed word separator twice)..." );

tempWord = "";
}
}

//Add last word if there is NO newline at the end
if ( !compareCharForNewline( input.charAt( inputLength-1 ) , newLine) )
returnedWords.add(tempWord);

Main.txtrLog.appendDebug("Extracted input: " + Arrays.toString(returnedWords.toArray()) );

return returnedWords;
}

private boolean compareCharForNewline( char charInput , String stringInput ){
String temp = Character.toString(charInput);
//Check if contains because newline(stringInput) can be 2 chars (on windows \r\n)
if ( stringInput.contains(temp)){
return true;
}else{
return false;
}
}



/*
* Returns a string of all characters that are between char 'between'
* Example input is: word\\ab"prefix" ,and between is '"'
* then the returned characters are 'prefix'
* Duplicates are not eliminated (we called other method to do it)
* If char between doesn't exist it returns empty string ""
* If there is an odd number of chars between it returns empty string ""
*/
public static String getCharactersFromOptions(String text, char between){
int i;
int j;
String temp = "";
String tempChars = "";
int length = text.length();
int tempLength;

if ( !(text != null && !text.isEmpty()) )
return "";

int count = text.length() - text.replace( String.valueOf(between), "").length();
if ( (count & 1) != 0 ){
Main.txtrLog.append("There can't be an odd number of '"+between+"' characters!" , 'E' );
return "";
}

for ( i=0; i<length; i++ ){
if ( text.charAt(i) == between ){
i++;
while ( (i)<length && text.charAt(i) != between ){
temp += text.charAt(i);
i++;
}
}

tempLength = temp.length();
if ( tempLength>0 )
for ( j=0; j<tempLength ;j++ )
tempChars += temp.charAt(j);

temp = "" ;
}

return removeDuplicatesString(tempChars);
}


/*
* Removes all duplicate chars from input string
*/
private static String removeDuplicatesString( String text ){
char[] chars = text.toCharArray();
Set<Character> charSet = new LinkedHashSet<Character>();
for (char c : chars) {
charSet.add(c);
}

StringBuilder sb = new StringBuilder();
for (Character character : charSet) {
sb.append(character);
}

return sb.toString();
}


}
39 changes: 29 additions & 10 deletions src/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public class Main {
//Basic elements
private static JFrame frmDecypherInStyle;
private static Settings windowSettings;
private static JTextArea txtWords;
private static InputConsole txtWords;
public static Logger txtrLog;
public static JLabel txtComplexity;
private static JLabel textCurrentMemUsage;
private static PrintWriter out;
private static Timer timer;
Expand Down Expand Up @@ -115,12 +116,11 @@ public void mouseReleased(MouseEvent e) {
frmDecypherInStyle.getContentPane().add(scrollPane);

//Add JTextArea for inputting words
txtWords = new JTextArea();
txtWords = new InputConsole();
txtWords.setForeground(Color.BLACK);
txtWords.setBackground(Color.LIGHT_GRAY);
scrollPane.setViewportView(txtWords);
txtWords.setFont(new Font("Monospaced", Font.PLAIN, 12));
txtWords.setLineWrap(true);
txtWords.setToolTipText("");

JButton btnClearWords = new JButton("Clear Words");
Expand Down Expand Up @@ -176,7 +176,7 @@ public void mouseReleased(MouseEvent e) {
try{
openPrintWriter("wordList.txt");
List<List<String>> tempOutput = new ArrayList<List<String>>();
tempOutput = ProcessInput.buildWords( ProcessInput.extractInput(txtWords.getText() , Settings.wordSeparator), Settings.wordCombinator , Settings.optionSeparator ) ;
tempOutput = ProcessInput.buildWords( txtWords.extractInput(txtWords.getText() , Settings.wordSeparator) ) ;
CombinatoricsPermutations.combinations2D( Helpers.convertListToStringArray(tempOutput) );
closePrintWriter();
txtrLog.appendToTextPanel("Finished!" , 'G');
Expand All @@ -193,10 +193,10 @@ public void mouseReleased(MouseEvent e) {

JLabel txtrOptions = new JLabel();
txtrOptions.setForeground(Color.LIGHT_GRAY);
txtrOptions.setText("Universal options:");
txtrOptions.setText("Post-process options:");
txtrOptions.setOpaque(false);
txtrOptions.setFont(new Font("Tahoma", Font.PLAIN, 11));
txtrOptions.setBounds(14, 179, 100, 18);
txtrOptions.setBounds(14, 179, 120, 18);
frmDecypherInStyle.getContentPane().add(txtrOptions);

JLabel lblMemoryUsage = new JLabel("Current RAM usage:");
Expand All @@ -216,7 +216,7 @@ public void mouseReleased(MouseEvent e) {
txtrT.setBackground(Color.LIGHT_GRAY);
txtrT.setToolTipText("Example: \\\\sbc\"1234567890AAa\" \\\\sec\"1234567890AAa\"");
txtrT.setFont(new Font("Monospaced", Font.PLAIN, 12));
txtrT.setBounds(124, 179, 640, 22);
txtrT.setBounds(134, 179, 630, 22);
frmDecypherInStyle.getContentPane().add(txtrT);

JLabel lblAlgorithmComplexity = new JLabel("Algorithm complexity:");
Expand All @@ -225,10 +225,16 @@ public void mouseReleased(MouseEvent e) {
lblAlgorithmComplexity.setBounds(14, 208, 110, 14);
frmDecypherInStyle.getContentPane().add(lblAlgorithmComplexity);

JLabel txtComplexity = new JLabel("Not working, to do in following updates");
txtComplexity = new JLabel("Click to refresh or click \"Refresh variables\"");
txtComplexity.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
updateComplexityText();
}
});
txtComplexity.setForeground(Color.LIGHT_GRAY);
txtComplexity.setFont(new Font("Monospaced", Font.PLAIN, 14));
txtComplexity.setBounds(124, 204, 640, 25);
txtComplexity.setBounds(134, 204, 630, 25);
frmDecypherInStyle.getContentPane().add(txtComplexity);

JLabel lblEstimatedMemoryUsage = new JLabel("Estimated filesize on disk:");
Expand All @@ -244,14 +250,15 @@ public void mouseReleased(MouseEvent e) {
frmDecypherInStyle.getContentPane().add(txtEstimatedMemUsage);

JButton btnRefreshVariables = new JButton("Refresh variables");
btnRefreshVariables.setEnabled(false);
btnRefreshVariables.setBackground(Color.DARK_GRAY);
btnRefreshVariables.setForeground(Color.GRAY);
btnRefreshVariables.setFocusPainted(false);
btnRefreshVariables.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
textCurrentMemUsage.setText("MB: " + getMemoryUsage());
updateComplexityText();

}
});
btnRefreshVariables.setFont(new Font("Tahoma", Font.PLAIN, 11));
Expand Down Expand Up @@ -342,4 +349,16 @@ public static void autoMemoryDetection(){
}


public static void updateComplexityText(){
try{
List<List<String>> tempOutput = new ArrayList<List<String>>();
tempOutput = ProcessInput.buildWords( txtWords.extractInput(txtWords.getText() , Settings.wordSeparator) ) ;
long tempCount = Helpers.totalExpectedGeneratedWords(tempOutput);
txtComplexity.setText( Helpers.getAlgorithmComplexityString(tempCount) + " Words:" + tempCount );
}catch(Exception exc){
txtrLog.append("Can't update algorithm complexity text, error: "+exc , 'E');
}
}


}
14 changes: 7 additions & 7 deletions src/main/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ This would generate: lovejesus , jesuslove , loveJesus , Jesuslove , loveicecrea



f) Universal options
f) Post-process options
These options only apply to ALREADY generated words.

--sb-%S Add specific string %S at beginning of each generated word example:
Expand Down Expand Up @@ -158,9 +158,9 @@ TO DO
SECTION 3: Explained complexity chart

Word count Complexity
Impossible
Very high
High
Medium
Low
Very low
>1000000000 Impossible
<1000000000 Very high
<500000000 High
<50000000 Medium
<5000000 Low
<100000 Very low
4 changes: 2 additions & 2 deletions src/main/methods/CombinatoricsPermutations.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class CombinatoricsPermutations {
private CombinatoricsPermutations(){};

/*
* You have to pass a clean string array, with no nulls
* Number of generated words: n = multiply number of words in 1D array block with each number of words from next block
* Method requires a clean string array (with no nulls)
* Number of generated words: multiply number of words in 1D array block with each number of words from next block
* Example input {{"apple","Apple"},{"banana","Banana",}} will result in: applebanana , appleBanana , Applebanana , AppleBanana
*/
public static void combinations2D(String [][] g) {
Expand Down
46 changes: 45 additions & 1 deletion src/main/methods/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Calendar;
import java.util.List;
import main.Help;
import main.Main;

public class Helpers {

Expand Down Expand Up @@ -74,7 +75,6 @@ private static ArrayList<String> clearList() {
*/

public static String[][] convertListToStringArray( List<List<String>> theStrings ){
//List<List<String>> theStrings ... coming from somewhere
String[][] stringsAsArray = new String[theStrings.size()][];
for (int i=0; i<theStrings.size();i++) {
List<String> aList = theStrings.get(i);
Expand All @@ -84,6 +84,50 @@ public static String[][] convertListToStringArray( List<List<String>> theStrings
}


public static long totalNumOfElementsIn2DList( List<List<String>> input, int rows ){
long counter = 0;
for( int i=0; i<rows; i++)
counter+=input.get(i).size();
return counter;
}


public static long factorial( long n ){
if(n==0)
return 1;
else return (n*factorial(n-1));

}


public static long totalExpectedGeneratedWords( List<List<String>> input ){
long multiplier = 1;
int rows = input.size();
for( int i=0; i<rows; i++){
multiplier*=input.get(i).size();
}
return factorial(rows)*multiplier;
}


public static String getAlgorithmComplexityString( long totalWords ){
if( totalWords<100000 )
return "Very low";
else if( totalWords<5000000 )
return "Low";
else if( totalWords<50000000 )
return "Medium";
else if( totalWords<500000000 )
return "High";
else if( totalWords<1000000000 )
return "Very High";
else
return "Almost Impossible!";
}




public static String getDate(){
return new SimpleDateFormat("[HH:mm:ss] ").format(Calendar.getInstance().getTime());
}
Expand Down
Loading

0 comments on commit b2eb122

Please sign in to comment.