Skip to content

Commit

Permalink
Update MagicFilter.java
Browse files Browse the repository at this point in the history
  • Loading branch information
rac021 authored Nov 10, 2017
1 parent 9f69561 commit d9d674a
Showing 1 changed file with 107 additions and 45 deletions.
152 changes: 107 additions & 45 deletions src/main/java/org/inra/yedgen/splitter/MagicFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class MagicFilter {
private final boolean isList ;

static Pattern patternFilter = Pattern.compile("\\{.*?\\}") ;
static Pattern listFilter = Pattern.compile("\\(.*?\\)") ;

static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") ;

Expand Down Expand Up @@ -60,24 +59,18 @@ public Map<Integer, String> getCodeQueryByVariables() {
return codeQueryByVariables ;
}

private void treateList( String expresion ) {
private void treateList( String expression ) {

int step = Integer.parseInt(expresion.toLowerCase().split(" peek ")[1].trim()) ;
int step = Integer.parseInt(expression.toLowerCase()
.split(" peek ")[1].trim()) ;

Matcher mListFilter = listFilter.matcher( expresion) ;

while ( mListFilter.find() ) {

List<String> asList = Arrays.asList(mListFilter.group()
.replace("(", "").replace(")","")
.trim().split(",")) ;
List<String> peeker = peeker( step, asList) ;
list.addAll(peeker) ;
List<String> asList = automateExtractor(expression) ;

List<String> peeker = peeker( step, asList) ;
list.addAll(peeker) ;

}

Matcher mPatternQuery = patternFilter.matcher( expresion) ;
List<String> vars = new ArrayList() ;
Matcher mPatternQuery = patternFilter.matcher( expression) ;
List<String> vars = new ArrayList() ;

while ( mPatternQuery.find()) {

Expand Down Expand Up @@ -119,7 +112,7 @@ private void treatRange( String expresion ) {

List<List<String>> peekDates = peekDates( expresion.split("\\{")[0].trim().replaceAll(" +", "") ,
step ,
range ) ;
range ) ;

list.addAll(peekDates) ;

Expand Down Expand Up @@ -170,33 +163,45 @@ private static List<List<String>> peekDates ( String stringDates ,
int step ,
Character range ) {

if ( range == null || range == 'y' || range == 'Y' ) {
if ( range == null ) {

List<Integer> dates = Stream.of(stringDates.split("_") )
.filter ( s -> ! s.isEmpty() )
.filter ( s -> ! s.isEmpty() )
.map(s -> Integer.parseInt(s))
.collect(toList()) ;

return peekDatesY( dates.get(0), dates.get(1), step ) ;
return peekDatesY ( dates.get(0), dates.get(1), step ) ;
}

else if ( range == 'm' || range == 'M' ) {
else switch (range) {

List<String> dates = Stream.of(stringDates.split("_") )
.filter ( s -> ! s.isEmpty() )
.collect(toList()) ;

return peekDatesM( dates.get(0) , dates.get(1) , step ) ;
}

else if ( range == 'd' || range == 'D' ) {

List<String> dates = Stream.of(stringDates.split("_") )
.filter ( s -> ! s.isEmpty() )
.collect(toList()) ;

return peekDatesD( dates.get(0), dates.get(1), step ) ;
}
case 'y': case 'Y':
{
List<Integer> dates = Stream.of(stringDates.split("_") )
.filter ( s -> ! s.isEmpty() )
.map(s -> Integer.parseInt(s))
.collect(toList()) ;
return peekDatesY ( dates.get(0), dates.get(1), step ) ;
}

case 'm': case 'M':
{
List<String> dates = Stream.of(stringDates.split("_") )
.filter ( s -> ! s.isEmpty() )
.collect(toList()) ;
return peekDatesM ( dates.get(0) , dates.get(1) , step ) ;
}

case 'd': case 'D':
{
List<String> dates = Stream.of(stringDates.split("_") )
.filter ( s -> ! s.isEmpty() )
.collect(toList()) ;
return peekDatesD ( dates.get(0), dates.get(1), step ) ;
}

default : break ;
}

return null ;

Expand All @@ -221,9 +226,9 @@ private static String toString( LocalDate date ) {
return formatter.format(date) ;
}

private static List<List<String>> peekDatesM ( String dateBeg ,
String dateEnd ,
int step ) {
private static List<List<String>> peekDatesM ( String dateBeg ,
String dateEnd ,
int step ) {

List<List<String>> dates = new ArrayList<>() ;
LocalDate _dateBeg = LocalDate.parse(dateBeg ) ;
Expand Down Expand Up @@ -258,9 +263,9 @@ private static List<List<String>> peekDatesM ( String dateBeg ,
return dates ;
}

private static List<List<String>> peekDatesD ( String dateBeg ,
String dateEnd ,
int step ) {
private static List<List<String>> peekDatesD ( String dateBeg ,
String dateEnd ,
int step ) {
step = step <= 0 ? 0 : step - 1 ;

List<List<String>> dates = new ArrayList<>() ;
Expand Down Expand Up @@ -294,9 +299,9 @@ private static List<List<String>> peekDatesD ( String dateBeg ,
return dates ;
}

private static List<List<String>> peekDatesY ( int dateBeg ,
int dateEnd ,
int step ) {
private static List<List<String>> peekDatesY ( int dateBeg ,
int dateEnd ,
int step ) {

List<List<String>> dates = new ArrayList<>() ;

Expand All @@ -310,5 +315,62 @@ private static List<List<String>> peekDatesY ( int dateBeg ,

return dates ;
}

private static List<String> automateExtractor( String expression ) {

int totalOpenParentheses = 0 ;

expression = expression.split(Pattern.quote("{"))[0].trim( )
.replaceFirst(Pattern.quote("("), "")
.replaceFirst(".$","").trim() ;

String[] tokens = expression.split("(?!^)") ;
StringBuilder sb = new StringBuilder() ;
List<String> expressionArray = new ArrayList<>() ;

for( String token : tokens ) {

if( token.equals("(")) {
totalOpenParentheses ++ ;
sb.append(token) ;
}
else if ( token.equals(",") ) {
if( totalOpenParentheses == 0 ) {
if( ! sb.toString().trim().isEmpty() ) {
expressionArray.add(sb.toString().trim()) ;
sb.setLength(0) ;
}
} else {
sb.append(token) ;
}
}
else if ( token.equals(")") ) {
totalOpenParentheses -- ;
if( totalOpenParentheses == 0 ) {
sb.append(token) ;
expressionArray.add(sb.toString().trim()) ;
sb.setLength(0) ;
}
else {
sb.append(token) ;
}
}
else {
sb.append(token) ;
}
}

if( ! sb.toString().trim().isEmpty()) {
expressionArray.add(sb.toString().trim()) ;
}
if( totalOpenParentheses != 0 ) {
expressionArray.clear() ;
}

return expressionArray ;

}

}


0 comments on commit d9d674a

Please sign in to comment.