I have a Type
class that will have many instances. I get the instances from a web service. The Type
class has a code
instance field that uniquely ids an instance:
@Data
@AllArgsConstructor
public class Type {
private String code;
}
Ten of these instances identify a special type. I have several places where I need to check if a Type
is one of these special types:
aType.equals(new Type("specialType1")) || aType.equals(new Type("specialType2")) ||...
In other places, I need to find where aType is not a special type. To centralize these specialized types and the check, I put it in the Type
class as follows:
@Data
@AllArgsConstructor
public class Type {
private static final List<Type> specialTypes = Arrays.asList(new Type("specialType1"),...);
private String code;
public boolean isSpecialType() {
return specialTypes.contains(this);
}
}
Should specialTypes and the logic to compare a type to special types be in the Type
class? Or would this type of logic be better in a service class? Should the special types be defined as enums?
Update
Type
instances are created via use of open feign:
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name="type-service")
public interface TypeClient {
@GetMapping("/type")
List<Type> getTypes();
}
Type
instances. (I updated the OP.) I'm not sure how feign would know to create aType
instance versus aSpecialType extends Type
instance. \$\endgroup\$SpecialType
, and simply implement it by those special types, later check asif(obj instanceOf SpecialType)
. Not sure what's this feign, but it comes with a good API, it would provide some creation API to allow you customize(take-control) object instancing. \$\endgroup\$Set<String>
because you need uniqueness for the strings. \$\endgroup\$Type
. Jackson does support deserialization into subclasses. So, subclassing is an option. \$\endgroup\$