Last active
January 29, 2016 20:43
-
-
Save imtiazahmad007/7a8557a584e8b9cb31d5 to your computer and use it in GitHub Desktop.
json-builder-api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import java.io.IOException; | |
import java.text.ParseException; | |
import builder.QueryBuilder; | |
public class App { | |
public static void main(String[] args) throws IOException, ParseException { | |
QueryBuilder builder = new QueryBuilder(); | |
builder.mustMatch("item", "milk") | |
.mustMatch("shelf-life", 5) | |
.shouldMatch("owner", "shop-right"); | |
System.out.println(builder.getQueryString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package elements; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
@JsonInclude(JsonInclude.Include.NON_NULL) | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class Bool extends Query{ | |
List<Must> must; | |
List<Should> should; | |
public Bool(){ | |
must = new ArrayList<Must>(); | |
should = new ArrayList<Should>(); | |
} | |
public List<Must> getMust() { | |
return must; | |
} | |
public void setMust(List<Must> must) { | |
this.must = must; | |
} | |
public List<Should> getShould() { | |
return should; | |
} | |
public void setShould(List<Should> should) { | |
this.should = should; | |
} | |
public void addMust(Must must) { | |
if (must == null) | |
this.must = new ArrayList<Must>(); | |
this.must.add(must); | |
} | |
public void addShould(Should should) { | |
if (should == null) | |
this.should= new ArrayList<Should>(); | |
this.should.add(should); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package elements; | |
import java.util.HashMap; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
@JsonInclude(JsonInclude.Include.NON_NULL) | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class Must { | |
HashMap<String, Object> match; | |
public Must(){ | |
match = new HashMap<String, Object>(); | |
} | |
public HashMap<String, Object> getMatch() { | |
return match; | |
} | |
public void setMust(HashMap<String, Object> match) { | |
this.match = match; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package elements; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
@JsonInclude(JsonInclude.Include.NON_NULL) | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class Query { | |
Bool bool; | |
public Bool getBool() { | |
return bool; | |
} | |
public void setBool(Bool bool) { | |
this.bool = bool; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package builder; | |
import java.util.HashMap; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import elements.Bool; | |
import elements.Must; | |
import elements.Query; | |
import elements.QueryContainer; | |
import elements.Should; | |
public class QueryBuilder { | |
private QueryContainer queryContainer; | |
private Query query; | |
private Bool bool; | |
private ObjectMapper mapper; | |
public QueryBuilder(){ | |
mapper = new ObjectMapper(); | |
this.queryContainer = new QueryContainer(); | |
this.bool = new Bool(); | |
this.query = new Query(); | |
query.setBool(bool); | |
queryContainer.setQuery(query); | |
} | |
public QueryBuilder mustMatch(String key, Object value) throws JsonProcessingException{ | |
Must must = new Must(); | |
HashMap<String, Object> conditions = new HashMap<String, Object>(); | |
conditions.put(key, value); | |
must.setMust(conditions); | |
this.bool.addMust(must); | |
mapper.writeValueAsString(query); | |
return this; | |
} | |
public QueryBuilder shouldMatch(String key, Object value) throws JsonProcessingException{ | |
Should should = new Should(); | |
HashMap<String, Object> conditions = new HashMap<String, Object>(); | |
conditions.put(key, value); | |
should.setShould(conditions); | |
this.bool.addShould(should); | |
mapper.writeValueAsString(query); | |
return this; | |
} | |
public String getQueryString() throws JsonProcessingException{ | |
String jsonInString = mapper.writeValueAsString(queryContainer); | |
return jsonInString; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package elements; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
@JsonInclude(JsonInclude.Include.NON_NULL) | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class QueryContainer { | |
Query query; | |
public Query getQuery() { | |
return query; | |
} | |
public void setQuery(Query query) { | |
this.query= query; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package elements; | |
import java.util.HashMap; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
@JsonInclude(JsonInclude.Include.NON_NULL) | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class Should { | |
HashMap<String, Object> match; | |
public Should(){ | |
match = new HashMap<String, Object>(); | |
} | |
public HashMap<String, Object> getMatch() { | |
return match; | |
} | |
public void setShould(HashMap<String, Object> match) { | |
this.match = match; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment