Add interfaces to groovy enums? - maven

I am unable to add interfaces to groovy enums.
example:
interface DeviceType.groovy
public interface DeviceType{
public String getDevice()
}
enum Device.groovy
public enum Devices implements DeviceType {
PHONE{
public String getDevice(){
return "PHONE"
}
}, ALARM {
public String getDevice(){
return "ALARM"
}
}
}
Simple Test
public class MainTest(){
public static void main(String [] args) {
System.out.println(Devices.PHONE.getDevice());
//should print phone
}
}
This is pseudo code, but a pretty good example.
When I use it with Groovy, I get an error from IntelliJ that I need to make the interface abstract.
If I make it abstract, maven won't compile saying it can't be both static and final.
Any tips?

You need to define getDevice() in the enum. Then you can override it, like this:
enum Device.groovy
public enum Devices implements DeviceType {
PHONE{
public String getDevice(){
return "PHONE"
}
}, ALARM {
public String getDevice(){
return "ALARM"
}
};
public String getDevice(){
throw new UnsupportedOperationException();
}
}

Since an enum is a class, and your class is implementing the interface, it needs to implement the function. Right now what you have is an enum that's NOT implementing the function, whose instances are each subclasses that do have a function of the same name. But since the enum itself doesn't have it, that's not good enough.
I'd like to offer my preferred syntax for a situation such as this:
public enum Devices implements DeviceType {
PHONE("PHONE"), ALARM("ALARM")
private final String devName
public String getDevice() { return devName }
private Devices(devName) { this.devName = devName }
}
Or, if the "device" is always going to match the name of the enum instance, you might as well just return that:
public enum Devices implements DeviceType {
PHONE, ALARM
public String getDevice() { return name() }
}

Related

Spring Boot YML Config Class Inheritance

Is it possible to use inheritance in Spring Boot YML configuration classes? If so, how would that be accomplished?
For example:
#ConfigurationProperties(prefix="my-config")
public class Config {
List<Vehicle> vehicles;
}
And the class (or interface) "Vehicle" has two implementations: Truck and Car. So the YAML might look like:
my.config.vehicles:
-
type: car
seats: 3
-
type: truck
axles: 3
I do not think it is possible (at least not that I know of). You could however design your code as follow:
Inject the properties into a Builder object
Define an object with all properties, which we'll call the VehicleBuilder (or factory, you choose its name).
The VehicleBuilders are injected from the Yaml.
You can then retrieve each builder's vehicle in a #PostConstruct block. The code:
#ConfigurationProperties(prefix="my-config")
#Component
public class Config {
private List<VehicleBuilder> vehicles = new ArrayList<VehicleBuilder>();
private List<Vehicle> concreteVehicles;
public List<VehicleBuilder> getVehicles() {
return vehicles;
}
public List<Vehicle> getConcreteVehicles() {
return concreteVehicles;
}
#PostConstruct
protected void postConstruct(){
concreteVehicles = vehicles.stream().map(f -> f.get())
.collect(Collectors.<Vehicle>toList());
}
}
The builder:
public class VehicleBuilder {
private String type;
private int seats;
private int axles;
public Vehicle get() {
if ("car".equals(type)) {
return new Car(seats);
} else if ("truck".equals(type)) {
return new Trunk(axles);
}
throw new AssertionError();
}
public void setType(String type) {
this.type = type;
}
public void setSeats(int seats) {
this.seats = seats;
}
public void setAxles(int axles) {
this.axles = axles;
}
}

Equivalent in java 8 from guava Enums.getIfPresent() that will return java.util.Optional?

I have the following code from an old group that is using guava Optional and Enums (getIfPresent).
// getNameAsString returns the string literal but I want to safely convert
// to an enum and return an java.util.Optional <MessageName>.
// MessageName is an enum
Optional<MessageName> msgName = Enums.getIfPresent(MessageName.class, obj.getMessage().getNameAsString());
How can I convert this to java 8? What is the equivalent of guava Enums.getIfPresent in java 8 that would return an java.util.Optional?
A more Stream-idiomatic way with some generics sprinkled on the method signature:
public static <T extends Enum<T>> Optional<T> valueOf(Class<T> clazz, String name) {
return EnumSet.allOf(clazz).stream().filter(v -> v.name().equals(name))
.findAny();
}
To test:
enum Test {
A;
}
public static void main(String[] args) {
Stream.of(null, "", "A", "Z").map(v -> valueOf(Test.class, v))
.forEach(v -> System.out.println(v.isPresent()));
}
Expected output:
false
false
true
false
I wanted to make a slightly cleaner version of #iamiddy's answer, but unfortunately i can't really put it in a comment, and it might not be OK to edit that answer.
This example is made to point out that there is no benefit in having a temporary value that might be null.
public static <T extends Enum<T>> Optional<T> getValueOf(Class<T> enumType, String name) {
try {
return Optional.of(Enum.valueOf(enumType, name));
} catch(IllegalArgumentException ex) {
return Optional.empty();
}
}
You can fake it with Java 8 as follows
import java.util.Optional;
Optional<MessageName> msgName = getValueOf(MessageName.class,obj.getMessage().getNameAsString());
public static Optional<MessageName> getValueOf(Class<MessageName> enumType, String name){
MessageName enumValue = null;
try{enumValue =Enum.valueOf(enumType, name);
}catch(IllegalArgumentException ex ){
//log this here
}
return Optional.ofNullable(enumValue);
}
The static method getValueOf deals with IllegalArgumentException

Com class not showing main interface

I have an interface and a class in the tyle ibrary that is produced the interface appears and so does the class but the class has no methods exposed on it. so I cannot create an Application object in say VBA in Microsoft Word and call the methods on it, does anyone know what is wrong?
[ComVisible(true), Guid("261D62BE-34A4-4E49-803E-CC3294613505")]
public interface IApplication
{
[DispId(207)]
[ComVisible(true)]
IExporter Exporter { get; }
[DispId(202)]
[ComVisible(true)]
object CreateEntity([In] kEntityType EntityType, [In] object aParent);
[DispId(208)]
[ComVisible(true)]
string GenerateSpoolFileSpec();
}
[ComVisible(true), Guid("BA7F4588-0B51-476B-A885-8E1436EA0768")]
public class Application : IApplication
{
protected Exporter FExporter;
public Application()
{
FExporter = new Exporter();
}
[DispId(207)]
[ComVisible(true)]
public IExporter Exporter
{
get {return FExporter;}
}
[DispId(202)]
[ComVisible(true)]
public object CreateEntity([In] kEntityType EntityType, [In] object aParent)
{
switch (EntityType)
{
case TypeJob:
return new Job(this, aParent);
case kappEntityType.kappEntityTypePage:
return new Page(this, aParent);
}
return null;
}
[DispId(208)]
[ComVisible(true)]
public string GenerateSpoolFileSpec()
{
string path = string.Format(JOB_PARAMS_PATH_SKELETON, SpoolFolder, DateTime.Now.ToString("yyyy.MM.dd.hh.mm.ss.fff"));
return path;
}
}
Got it, don’t let dotnet handle it for you on the interface put an interfacetype e.g.
[ComVisible(true), Guid("261D62BE-34A4-4E49-803E-CC3294613505"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
On the class use a classinterface e.g
[ComVisible(true), Guid("BA7F4588-0B51-476B-A885-8E1436EA0768"), ClassInterface(ClassInterfaceType.None)]

How Get data from a derived class?

Get data from a derived class..
My samples.
public class Maintest
{
public string name = "2";
}
public class test : Maintest
{
string bla = name;
}
or
public class test : Maintest
{
test child = new test();
string bla = child.name;
}
Please reply
or
Share a link to explore
For example there is the main class
and I have a derived class that will output data of the first class.
As an example, I just wanted to pass the value of the derivative in the main class. For a proper understanding
If you return the field from a property, it might look a little something like this.
using System;
public class Program
{
public void Main()
{
var test = new Test();
Console.WriteLine(test.greeting);
}
}
// make this abstract if you're never directly instantiate MainTest
public abstract class MainTest
{
public string name = "world";
}
public class Test : MainTest
{
public string greeting {get { return "Hello " + name;}}
}
http://dotnetfiddle.net/R8kwh3
Also, you can enforce a contract by doing something like
public abstract class MainTest
{
public string name = "world";
// create an abstract property to ensure it gets implemented in the inheriting class
public abstract string greeting {get; private set;}
}
public class Test : MainTest
{
public override string greeting {get { return "Hello " + name;}}
}
Maybe you want get data in method? Therefore, you can use this:
public class test : Maintest
{
public string GetData()
{
return name;
}
}

Entity framework: ObjectContext and inheritance

I need to have a CRUd operations on my class (CompetenceSpecific).
Competence has three derived classes - CompetenceFunction, CompetenceArea and CompetenceSpecifc
The error I recieved:
There are no EntitySets defined for the specified entity type 'CompetencyManagement.Domain.Entities.CompetenceFunction'. If 'CompetencyManagement.Domain.Entities.CompetenceFunction' is a derived type, use the base type instead. Parameter name: TEntity
How should I correct this? Please suggest a solution that would solve my problem. Thanks
Please check the code below, I removed some parts of the code for simplicity.
--MODEL
public class Competence
{
public int CompetenceID { get; set; }
public int CourseID { get; set; }
...
}
public class CompetenceFunction : Competence
{
}
--REPOSITORY and interfaces
public interface IRepository<T> where T : class
{
T GetById(object id);
IEnumerable<T> GetAll();
IEnumerable<T> Query(Expression<Func<T, bool>> filter);
void Add(T entity);
void Remove(T entity);
}
public abstract class Repository<T> : IRepository<T>
where T : class
{
protected IObjectSet<T> _objectSet;
public Repository(ObjectContext context)
{
_objectSet = context.CreateObjectSet<T>();
}
...
}
public class CompetenceFunctionRepository : Repository<CompetenceFunction>
{
public CompetenceFunctionRepository(ObjectContext context)
: base(context)
{
}
public override CompetenceFunction GetById(object id)
{
return _objectSet.SingleOrDefault(s => s.CompetenceID == (int)id);
}
}
--UNIT oF WORK
public interface IUnitOfWork
{
IRepository<CompetenceFunction> CompetenceFunctions { get; }
IRepository<CompetenceArea> CompetenceAreas { get; }
IRepository<CompetenceSpecific> CompetenceSpecifics { get; }
void Commit();
}
public class UnitOfWork : IUnitOfWork, IDisposable
{
private CompetenceFunctionRepository _competencefunction;
private CompetenceAreaRepository _competencearea;
private CompetenceSpecificRepository _competencespecifc;
public UnitOfWork(ObjectContext context)
{
if (context == null)
{
throw new ArgumentNullException("Context was not supplied");
}
_context = context;
}
#region IUnitOfWork Members
public IRepository<CompetenceFunction> CompetenceFunctions
{
get
{
if (_competencefunction == null)
{
_competencefunction = new CompetenceFunctionRepository(_context);
}
return _competencefunction;
}
}
public IRepository<CompetenceArea> CompetenceAreas
{
get
{
if (_competencearea == null)
{
_competencearea = new CompetenceAreaRepository(_context);
}
return _competencearea;
}
}
public IRepository<CompetenceSpecific> CompetenceSpecifics
{
get
{
if (_competencespecifc == null)
{
_competencespecifc = new CompetenceSpecificRepository(_context);
}
return _competencespecifc;
}
}
--Im getting an error in this part of Repository
public Repository(ObjectContext context)
{
_objectSet = context.CreateObjectSet<T>();
}
There are no EntitySets defined for the specified entity type 'CompetencyManagement.Domain.Entities.CompetenceFunction'. If 'CompetencyManagement.Domain.Entities.CompetenceFunction' is a derived type, use the base type instead. Parameter name: TEntity
Here's how I implement in the controller
private IUnitOfWork _unitOfWork;
var a = _unitOfWork.CompetenceFunctions.GetAll();
return View(a);
You have to get derived type by the OfType function, e.g.
context.CreateObjectSet<Competence>().OfType<CompetenceFunction>()
In your case that would mean that there is only a CompetenceRepository that serves all derivatives of Competence.
Edit
(After your comment)
First, UoW is meant for temporarily storing changes that should be dealt with in one batch (like changes to be committed to the database). GetAll and similar functions are repository stuff.
But do you need repositories? I like this post. When beginning to know EF, I would focus on the ins and outs of EF without getting distracted too much by surrounding architecture. E.g. start with services that at the inside communicate directly with the context and expose methods like GetCompetenceFunctions, GetCompetenceAreas (using OfType), and SaveCompetenceFunction, ....
You can address these service methods directly from action methods in the MVC controllers.

Resources