Java casting enum to a class - java

I have a class which is identical to enum. The only difference is that I create the class so that I can dynamically create the enums. What I want is to override the cast operation of enum so that I can give the enum instance to a method where it gets the class instance.
Example:
public enum SpecialEnum {
FIRST("First"), SECOND("Second");
private String name;
SpecialEnum(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class SpecialClass {
private String name;
public SpecialClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public void displayName(SpecialClass specialClass) {
System.out.println(specialClass.getName());
}
Lets say that the SpecialClass instances are coming from a server where I display them. But I also want to use my pre-defined Special enum classes as well. I know that I could create static instances of SpecialClass and use them but not that it looks messy, also using enums are beneficial to my occasion as well. Is there a way to override casting operation of the enum class of a work around maybe?

Extract an interface:
public interface Named {
public String getName();
}
public class SpecialClass implements Named {
private String name;
public SpecialClass(String name) {
this.name = name;
}
#Override
public String getName() {
return name;
}
}
public enum SpecialEnum implements Named {
FIRST("First"), SECOND("Second");
private String name;
SpecialEnum(String name) {
this.name = name;
}
#Override
public String getName() {
return name;
}
}
public void displayName(Named specialClass) {
System.out.println(specialClass.getName());
}

Related

Adding test in JUnit that fails if Serialized Object changes

I have the following class that is created with the usage of a builder:
public class Foo {
private String name;
private int age;
public String getName() { return name; }
public int getAge() { return age; }
private Foo(Builder b) {
name = b.name;
age = b.age;
}
public static final class Builder {
private String name;
private int age;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
}
}
Now I want to add a JUnit test for this where if this class were to change (via new field were to be added, some other changes made to this) that would also be reflected once the class got serialized, I want that test to fail to catch that change. I am not aware of any libraries that can do this, how can this be done?

Subclassing an abstract class with an unknown variable type

I'm creating a reusable property class that I want to base on an abstract class that I will subclass with different variable types in Java.
public abstract class Property{
protected String name;
protected <type> type;
/*
Here is where the problem is, The abstract class won't know what the object type is -- So that if I derive a class of StringProperty whose type is String, how do I implement my setters and getters? in the parent class?*/
public void setName(String n){
name= n;
}
public String getName(){
return name;
}
}//end class
public class StringProperty extends Property{
super.setName("email");
super.setType("String");
}//end class
I want to genericize the abstract parent so it can be derived as StringProperty, IntProperty, BoolProperty, etc. and set the derived type at compile time, but I can't figure it out.
I want each Property Instance to have three properties: Name, type, and Value.
inherited by the child classes? The getter method needs to know the type beforehand
you mean generic type ?
public abstract class Property<T> {
protected String name;
protected T type;
public T getType() {
return type;
}
public void setType(T type) {
this.type = type;
}
}
public class StringProperty extends Property<String> {
...
}
You should use constructor in your subclasses instead of setter like in StrinProperty.
I don't understand why you need the type variable. So use your asked value variable. Because it's a bit obvious that a StringProperty handles strings and has a String value.
public abstract class Property<T> {
private String name;
private T value;
protected Property(String name, T value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
public class StringProperty extends Property<String> {
public StringProperty(String name, String value) {
super(name, value);
}
}

Select appropriate enum class based on class member

I have an absract class that describes a general functionality of children classes.
When I initialize a child class I want to set a specific enum class as a member on the parent abstract class.
How can I do that?
Example:
AbstractFunctionality.java
public abstract class AbstractFunctionality {
protected String Name;
protected String Surname;
// specific enum class
public AbstractFunctionality(String Name, String Surname){
this.Name = Name;
this.Surname = Surname;
}
}
Child1.java
public class Child1 extends AbstractFunctionality {
public Child1(){
super("Jane","Austen");
}
}
How can I specify that I want the public enum Writers in my Child1 class?
The simpler approach is just add the enum type as the type of the field parameter of the abstract class:
public abstract class AbstractFunctionality {
protected String Name;
protected String Surname;
Writers writers;
public AbstractFunctionality(String Name, String Surname, Writers writers){
this.Name = Name;
this.Surname = Surname;
this.writers = writers;
}
}
the subclass:
public class Child1 extends AbstractFunctionality {
public Child1(){
super("Jane","Austen", Writers.SOME_FIELD);
}
}
Alternatively, you can make your enum Writers implement a more general interface let us say IWriters
public abstract class AbstractFunctionality {
protected String Name;
protected String Surname;
protected IWriters writers;
public AbstractFunctionality(String Name, String Surname, IWriters writers){
this.Name = Name;
this.Surname = Surname;
this.writers = writers;
}
}
The interface:
public interface IWriters {
...
}
the enum:
public enum Writers implements IWriters{
...
}
The benefit of this approach is that you can have different enums types implementing the same interface, and therefore they can also be used on the abstract class.
Maybe you can set generic in abstract class
public abstract class AbstractFunctionality<T extends Enum<T>> {
protected String Name;
protected String Surname;
T specificEnum
public AbstractFunctionality(String Name, String Surname,T specificEnum){
this.Name = Name;
this.Surname = Surname;
this.specificEnum=specificEnum;
}
In the sub class
public class Child1 extends AbstractFunctionality<Writers> {
public Child1(){
super("Jane","Austen",Writers.POEM);
}
}
}

Overrride global jackson config

Im my app null values will not be serialized to json, and its ok. But in one specific case, I'd like to have null values send to client. How could I achieve that ?
class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
for this class I tried with JsonInclude.Always but its defualt value that gets overriden in config later on.
Use JsonInclude annotation. Example:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
System.out.println(mapper.writeValueAsString(new User()));
}
}
#JsonInclude
class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Above code prints:
{"name":null}
Default value in JsonInclude is ALWAYS:
/**
* Inclusion rule to use for instances (values) of types (Classes) or
* properties annotated.
*/
public Include value() default Include.ALWAYS;
Other option is to use JsonSerialize annotation:
#JsonSerialize(include = Inclusion.ALWAYS)
class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Result is the same as for JsonInclude.
Try
#JsonInclude(JsonInclude.Include.ALWAYS)

[JAVA]How to get generic class typed name

I would like to get E class name in String value with java reflection.
For example, if i create Node typed in Person, getClassName() has to return "Person"
Someone can help me?
public class Node<E extends AbstractNode> {
String getClassName() {
String name = ??
}
private String alias;
public Node() {
}
}
public abstract class AbstractNode {
}
public class Person extends AbstractNode {
private String name;
private String surname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
Add a field to your Node class
private static final Class<T> type;
And add this to your constructor signature
public Node(Class type) {
this.type = type;
}
Lastly, create the getClassName method in your Node class:
public String getClassName(){
return this.type.getName();
}
Edit:
As a sidenote, your AbstractNode class is not really necessary (it doesn't do anything) and therefore neither is the extend in Person.

Categories