How to print enum description - java

Im creating a Java application, I used enum to create movie category. When I input MovieCategory.WAR I would like to see War movie(My description) instead of WAR. How is it possible? I tried MovieCategory.WAR.getDescription() but does't work.
public enum MovieCategory {
COMEDY("Comedy"), HORROR("Horror"), SCIFI("Sci-Fi"),
ACTION("Action movie"), ROMANTIC("Romantic"),
CLASSIC("Classic"), WAR("War movie");
private final String description;
MovieCategory(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
public class MovieManager {
private List<Movie> movieList;
public MovieManager() {
this.movieList = new ArrayList<>();
movieList.add(new Movie("Simple movie", MovieCategory.WAR,"Testing description.",167,12));
(...)

The enum works correctly:
public static void main(String[] args) {
// Displays: Comedy
System.out.println(COMEDY.getDescription());
// Displays: COMEDY
System.out.println(COMEDY);
}
Or, maybe you want the toString method to use description?
public enum MovieCategory {
COMEDY("Comedy");
private final String description;
MovieCategory(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
#Override
public String toString() {
return getDescription();
}
public static void main(String[] args) {
// Displays: Comedy
System.out.println(COMEDY.getDescription());
// Displays: Comedy
System.out.println(COMEDY);
}
}
UPDATE #3: It seems the issue is the JSON response. If you want the description to be returned, you can annotate the getDescription method wtih #JsonValue.
import com.fasterxml.jackson.annotation.JsonValue;
public enum MovieCategory {
WAR("War movie");
private final String description;
MovieCategory(String description) {
this.description = description;
}
#JsonValue
public String getDescription() {
return description;
}
#Override
public String toString() {
return getDescription();
}
public static void main(String[] args) {
// Displays: Comedy
System.out.println(WAR.getDescription());
// Displays: Comedy
System.out.println(WAR);
}
}

Related

java.lang.NullPointerException when calling getters

This's an exercise from my Java course, I need to print out "Rouge" on the screen but something goes wrong and I can't figure out what it is. Help me, please!
Here is my code:
public class Cercle {
public Couleur couleur;
public static void main(String[] args) {
Cercle cercle = new Cercle();
cercle.couleur.setDescription("Rouge");
System.out.println(cercle.couleur.getDescription());
}
public void Cercle() {
this.couleur = new Couleur();
}
public class Couleur {
String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
}

How to get a set of an instance variable using Stream

I have a class called Change. This class has two String variables called format and description, with normal getters and setters.
If I have a ArrayList<Change> changes how do I get a Set of all the different Formats using Java Streams?
You use Stream#map method in order to get a Stream of strings (based on the "method" you passed as argument), and then you collect it using Stream#collect method.
For example:
public class Change {
private String format;
private String description;
public Change(String format, String description) {
this.format = format;
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public static void main(String[] args) {
List<Change> list = new ArrayList<>();
list.add(new Change("format1", "descr1"));
list.add(new Change("formatelse", "descrelse"));
Set<String> formats = list.stream().map(Change::getFormat).collect(Collectors.toSet());
}
}

Android Retrofit - How to seperate classes from JSON Array

trying to access JSON data from the following:
{"actions":[{"actionType":0,"email":"contact#tonyspizza.com","faIcon":"fa-envelope",
"name":"Contact Us","subject":"Email from Tony's Pizza App"},
{"actionType":2,"faIcon":"fa-phone","name":"Call Us","number":"5204558897"}],
"total":2}
I'm trying to use retrofit to access the 'actions' as each individual classes. (i.e. ActionEmail, ActionPhone, etc). I cannot figure out a way to separate these into separate classes and not have one class with all the properties.
Thanks in advance!
Call<ActionWrapperObject> getActions(// Put your api call body in there);
Here is your ActionWrapperObject
public class ActionWrapperObject {
ArrayList<ActionModel> actions;
public ArrayList<ActionModel> getActions() {
return actions;
}
public void setActions(ArrayList<ActionModel> actions) {
this.actions = actions;
}
}
Here is your ActionModel
public class ActionModel {
int actionType;
String email;
String faIcon;
String name;
String subject;
public int getActionType() {
return actionType;
}
public void setActionType(int actionType) {
this.actionType = actionType;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFaIcon() {
return faIcon;
}
public void setFaIcon(String faIcon) {
this.faIcon = faIcon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
You in your response
Your api call.enqueue(new Callback<ActionWrapperObject>() {
#Override
public void onResponse(Call<ActionWrapperObject> call, Response<ActionWrapperObject> response) {
ActionWrapperObject actionWrapperObj= response.body();
if (actionWrapperObj!= null) {
ArrayList<ActionModel> actionModelList= actionWrapperObj.getActions();
//Here you got the list of actions. Do what ever you want with them. You can
// differentiate each action on its type.
}
}
What I infer is you want to generate fields of the ActionModel class dynamically. You can refer to generating JSON pojo dynamically using reflection.

Adobe Flash Builder does not refresh a value object

I'm currently facing a problem with Adobe Flash Builder, I work on a project with a Java core and services to communicate with a Flex client. (BlazeDS channel)
I decided to remove an array list in an object because it was no longer useful, so I commented the following lines in my object value to remove the ArrayList named "unitFactors"
public class UnitValue extends PhysicalQuantityObjectValue{
String symbol;
String name;
String description;
String physicalQuantityName;
//UnitFactorValue[] unitFactors;
UnitConstraintValue unitConstraint;
public UnitValue(AdnUnit unit) {
super(unit.getId());
//Constructor code ...
}
public UnitValue() {}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPhysicalQuantityName() {
return physicalQuantityName;
}
public UnitValue(String symbol, String name, String description,
String physicalQuantityName,
UnitConstraintValue unitConstraint) {
super();
this.symbol = symbol;
this.name = name;
this.description = description;
this.physicalQuantityName = physicalQuantityName;
//this.unitFactors = unitFactors;
this.unitConstraint = unitConstraint;
}
public void setPhysicalQuantityName(String physicalQuantityName) {
this.physicalQuantityName = physicalQuantityName;
}
//public UnitFactorValue[] getUnitFactors() {
//return unitFactors;
//}
//public void setUnitFactors(UnitFactorValue[] unitFactors) {
//this.unitFactors = unitFactors;
//}
public UnitConstraintValue getUnitConstraint() {
return unitConstraint;
}
public void setUnitConstraint(UnitConstraintValue unitConstraint) {
this.unitConstraint = unitConstraint;
}
Well, no problem with the Java part. I tested it with SoapUI, everything's fine.
But when I switch on my Flex client code to refresh the valueObjects, it appears impossible to refresh UnitValue, unitFactors is still there, and it's obviously impossible to make my application work without errors on that specific point ... Can't find getUnitFactors().
Is someone facing the same problem with AdobeFlashBuilder?

How to get values from JSON pojo

I am very new to JSON and jackson, currently I have pojo files and I am trying to get the data and store it in an array. for example I want to extract Network name and store it an array and later display or compare it with live site data.
here is the main pojo file -
public class JsonGen{
private String _type;
private List cast;
private List clips;
private Common_sense_data common_sense_data;
private String common_sense_id;
private List crew;
private String description;
private List episodes;
private Number franchise_id;
private List genres;
private String guid;
private Images images;
private boolean is_locked;
private boolean is_mobile;
private boolean is_parental_locked;
private String kind;
private List mobile_networks;
private String most_recent_full_episode_added_date;
private String name;
private List networks;
private List platforms;
private List ratings;
private String release_date;
private List season_filters;
private String slug;
private String tms_id;
public String get_type(){
return this._type;
}
public void set_type(String _type){
this._type = _type;
}
public List getCast(){
return this.cast;
}
public void setCast(List cast){
this.cast = cast;
}
public List getClips(){
return this.clips;
}
public void setClips(List clips){
this.clips = clips;
}
public Common_sense_data getCommon_sense_data(){
return this.common_sense_data;
}
public void setCommon_sense_data(Common_sense_data common_sense_data){
this.common_sense_data = common_sense_data;
}
public String getCommon_sense_id(){
return this.common_sense_id;
}
public void setCommon_sense_id(String common_sense_id){
this.common_sense_id = common_sense_id;
}
public List getCrew(){
return this.crew;
}
public void setCrew(List crew){
this.crew = crew;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public List getEpisodes(){
return this.episodes;
}
public void setEpisodes(List episodes){
this.episodes = episodes;
}
public Number getFranchise_id(){
return this.franchise_id;
}
public void setFranchise_id(Number franchise_id){
this.franchise_id = franchise_id;
}
public List getGenres(){
return this.genres;
}
public void setGenres(List genres){
this.genres = genres;
}
public String getGuid(){
return this.guid;
}
public void setGuid(String guid){
this.guid = guid;
}
public Images getImages(){
return this.images;
}
public void setImages(Images images){
this.images = images;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public boolean getIs_mobile(){
return this.is_mobile;
}
public void setIs_mobile(boolean is_mobile){
this.is_mobile = is_mobile;
}
public boolean getIs_parental_locked(){
return this.is_parental_locked;
}
public void setIs_parental_locked(boolean is_parental_locked){
this.is_parental_locked = is_parental_locked;
}
public String getKind(){
return this.kind;
}
public void setKind(String kind){
this.kind = kind;
}
public List getMobile_networks(){
return this.mobile_networks;
}
public void setMobile_networks(List mobile_networks){
this.mobile_networks = mobile_networks;
}
public String getMost_recent_full_episode_added_date(){
return this.most_recent_full_episode_added_date;
}
public void setMost_recent_full_episode_added_date(String most_recent_full_episode_added_date){
this.most_recent_full_episode_added_date = most_recent_full_episode_added_date;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public List getNetworks(){
return this.networks;
}
public void setNetworks(List networks){
this.networks = networks;
}
public List getPlatforms(){
return this.platforms;
}
public void setPlatforms(List platforms){
this.platforms = platforms;
}
public List getRatings(){
return this.ratings;
}
public void setRatings(List ratings){
this.ratings = ratings;
}
public String getRelease_date(){
return this.release_date;
}
public void setRelease_date(String release_date){
this.release_date = release_date;
}
public List getSeason_filters(){
return this.season_filters;
}
public void setSeason_filters(List season_filters){
this.season_filters = season_filters;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
public String getTms_id(){
return this.tms_id;
}
public void setTms_id(String tms_id){
this.tms_id = tms_id;
}
}
here is the Network Pojo class -
public class Networks{
private String banner;
private String description;
private boolean is_locked;
private String logo;
private String name;
private String network_analytics;
private Number network_id;
private String slug;
private String thumbnail_url;
private String url;
public String getBanner(){
return this.banner;
}
public void setBanner(String banner){
this.banner = banner;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public String getLogo(){
return this.logo;
}
public void setLogo(String logo){
this.logo = logo;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getNetwork_analytics(){
return this.network_analytics;
}
public void setNetwork_analytics(String network_analytics){
this.network_analytics = network_analytics;
}
public Number getNetwork_id(){
return this.network_id;
}
public void setNetwork_id(Number network_id){
this.network_id = network_id;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
public String getThumbnail_url(){
return this.thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url){
this.thumbnail_url = thumbnail_url;
}
public String getUrl(){
return this.url;
}
public void setUrl(String url){
this.url = url;
}
}
and here is my code through which I am trying to extract the network names -
public class util {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
List<JsonGen> jsongenShow = null;
String url1 = "http://www.dishanywhere.com/radish/v20/dol/home/carousels/shows.json";
getShowNWGopherParser(nwork, url1);
}
public static String[] getShowNWGopherParser (List<Networks> nwork, String url ) throws JsonParseException, JsonMappingException, IOException
{
URL jsonUrl = new URL(url);
ObjectMapper objmapper = new ObjectMapper();
//objmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
nwork = objmapper.readValue(jsonUrl, new TypeReference<List<Networks>>() {});
String [] shows = new String [nwork.size()];
int i = 0;
for(Networks element : nwork) {
shows[i++]=element.getUrl();
}
for(int j =0; j<shows.length;j++)
{
System.out.println(shows[j]);
}
return shows;
}
}
and here is the error -
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "networks" (class featureshows.Networks), not marked as ignorable (10 known properties: , "logo", "slug", "name", "banner", "network_id", "url", "network_analytics", "description", "thumbnail_url", "is_locked"])
at [Source: http://www.dishanywhere.com/radish/v20/dol/home/carousels/shows.json; line: 1, column: 15] (through reference chain: featureshows.Networks["networks"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:568)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:650)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:830)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:310)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2563)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1789)
at functions.util.getShowNWGopherParser(util.java:77)
at functions.util.main(util.java:31)
The reason is that you're using a regular List interface for your list of networks instead of the generic version with the bounds for the expected type.
Try changing the field declaration from:
private List networks;
to
private List<Networks> networks;
While you're at it, it looks like you're using the regular List interface pretty much everywhere. You'll probably run into more issues if you don't convert them all to include the type you expect in the list.
Essentially, you're not providing enough information about the type of object you expect in the list for jackson to figure out what to populate it with. You can read more about generics here: http://docs.oracle.com/javase/tutorial/extra/generics/intro.html
EDIT:
It looks (from your comment) that you've already tried disabling checks for unknown properties, but try adding this annotation to your Networks class:
#JsonIgnoreProperties(ignoreUnknown = true)

Categories