Development docs
  • Introduction
  • General information
    • Development Ecosystem
    • Documentation
    • Testing
    • Continuous Integration
    • Code Style
    • Paradigms
    • Design Patterns
    • Architecture
    • Refactoring
    • Date Formats
  • Various
    • Technologies and services
      • Travis
      • GitHub
    • Databases
      • Database Kinds
      • Style Guide
      • Joins
    • Web
      • Template View
      • Interactive View
      • Open Graph
      • Twitter Card
    • Parsers
    • Regular Expression
    • File Formats
      • Properties
    • Logging
  • git
    • Configuration files
      • gitignore
      • gitattributes
    • Gitflow
    • Tools
  • Web
    • REST
    • SOAP
  • Architecture
    • SOA
  • Object Oriented Programming
    • Returns
  • Jenkins
    • Pipeline
      • Steps
      • Environment
      • Notifications
      • Scripts
  • Java
    • Environment
      • Development Ecosystem
      • IDE
    • General
    • Interfaces and Generics for a Service
      • Dependencies
      • Type Errors
      • Nested Type Errors
    • Creating New Instances Dynamically
      • Using Classes
      • Using Providers
Powered by GitBook
On this page
  • Type Errors
  • Dependency Expecting a Child
  • Solving Type Errors
  • Transforming the Input
  • Adding a Type

Was this helpful?

  1. Java
  2. Interfaces and Generics for a Service

Type Errors

Type Errors

Dependency Expecting a Child

We can't use the repository for finding data:

public ModelObject find(final ModelObject sample) {
   // ERROR: ModelObject is not ModelObjectEntity
   return repository.read(sample);
}

Solving Type Errors

Transforming the Input

If we want to keep the interface then we need to transform the input object into a valid type.

There are two cases here:

  • We have received an instance of the expected type, but the interface hides it

  • We have received an instance of another type

In the first case we only need to cast the input. But in the other one we have to copy the properties into a new instance.

public ModelObject find(final ModelObject sample) {
   final ModelObjectEntity entitySample;

   entitySample = toEntity(sample);

   return repository.read(sample);
}

private ModelObjectEntity toEntity(final ModelObject sample) {
   final ModelObjectEntity entity;

   if(sample instanceof ModelObjectEntity) {
      // The sample matches the expected type
      entity = (ModelObjectEntity) sample;
   } else {
      entity = new ModelObjectEntity();
      entity.setName(sample.getName());
   }

   return entity;
}

Adding a Type

If we can change the interface another option is adding the type by using generics.

public interface ModelObjectService<T extends ModelObject> {

   public T find(final T sample);

}

While this solves the problems, it also means that we will need a version of the service for each type.

final ModelObjectService<ModelObjectEntity> = new ModelObjectService<>(repository);
final ModelObjectService<ModelObjectAdditionalField> = new ModelObjectService<>(dependency);

When using dependency injection we will need to take care of the type, making it harder to swap one version of the service for another.

PreviousDependenciesNextNested Type Errors

Last updated 6 years ago

Was this helpful?