Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.67 KB | None | 0 0
  1. import com.fasterxml.jackson.annotation.JsonSubTypes;
  2. import com.fasterxml.jackson.annotation.JsonTypeInfo;
  3. import jakarta.validation.constraints.NotBlank;
  4. import jakarta.validation.constraints.NotNull;
  5. import org.hibernate.validator.constraints.Length;
  6.  
  7. public class ItemDTO {
  8.     private interface Id {
  9.         @NotNull
  10.         Long getId();
  11.     }
  12.  
  13.     private interface Name {
  14.         @NotBlank
  15.         @Length(min = 1, max = 64)
  16.         String getName();
  17.     }
  18.  
  19.     private interface VisibleName {
  20.         @NotBlank
  21.         @Length(min = 1, max = 64)
  22.         String getVisibleName();
  23.     }
  24.  
  25.     private interface Cost<T> {
  26.         @NotNull
  27.         T getCost();
  28.     }
  29.  
  30.     private interface Type {
  31.         @NotNull
  32.         Types getType();
  33.     }
  34.  
  35.     public enum Types {
  36.         MONEY(Constants.MONEY_NAME), TRASH(Constants.TRASH_NAME);
  37.  
  38.         Types(String name) {
  39.             if (!name.equals(this.name()))
  40.                 throw new IllegalArgumentException();
  41.         }
  42.  
  43.         public static class Constants {
  44.             public static final String MONEY_NAME = "MONEY";
  45.             public static final String TRASH_NAME = "TRASH";
  46.         }
  47.  
  48.         @Override
  49.         public String toString() {
  50.             return super.toString();
  51.         }
  52.     }
  53.  
  54.     public enum Request {
  55.         ;
  56.  
  57.         public static class Name implements ItemDTO.Name {
  58.             String name;
  59.  
  60.             protected Name() {
  61.             }
  62.  
  63.             public Name(String name) {
  64.                 this.name = name;
  65.             }
  66.  
  67.             @Override
  68.             public String getName() {
  69.                 return name;
  70.             }
  71.         }
  72.  
  73.         @JsonTypeInfo(
  74.                 use = JsonTypeInfo.Id.NAME,
  75.                 property = "type",
  76.                 visible = true)
  77.         @JsonSubTypes({
  78.                 @JsonSubTypes.Type(value = CreateMoney.class, name = Types.Constants.MONEY_NAME),
  79.                 @JsonSubTypes.Type(value = CreateTrash.class, name = Types.Constants.TRASH_NAME)
  80.         })
  81.         public static abstract class CreateItem implements VisibleName, Type {
  82.             String visibleName;
  83.             Types type;
  84.  
  85.             protected CreateItem() {
  86.             }
  87.  
  88.             protected CreateItem(String visibleName) {
  89.                 this.visibleName = visibleName;
  90.             }
  91.  
  92.             @Override
  93.             public String getVisibleName() {
  94.                 return visibleName;
  95.             }
  96.  
  97.             @Override
  98.             public Types getType() {
  99.                 return type;
  100.             }
  101.         }
  102.  
  103.         public static class CreateTrash extends CreateItem {
  104.             protected CreateTrash() {
  105.             }
  106.  
  107.             public CreateTrash(String visibleName) {
  108.                 super(visibleName);
  109.             }
  110.  
  111.         }
  112.  
  113.         public static class CreateMoney extends CreateItem implements Cost<MoneyDTO.Request.Create> {
  114.             MoneyDTO.Request.Create cost;
  115.  
  116.             protected CreateMoney() {
  117.             }
  118.  
  119.             public CreateMoney(MoneyDTO.Request.Create cost, String visibleName) {
  120.                 this.cost = cost;
  121.                 this.visibleName = visibleName;
  122.             }
  123.  
  124.             @Override
  125.             public MoneyDTO.Request.Create getCost() {
  126.                 return cost;
  127.             }
  128.         }
  129.     }
  130.  
  131.     public enum Response {
  132.         ;
  133.  
  134.         public static class Name implements ItemDTO.Name {
  135.             String name;
  136.  
  137.             protected Name() {
  138.             }
  139.  
  140.             public Name(String name) {
  141.                 this.name = name;
  142.             }
  143.  
  144.             @Override
  145.             public String getName() {
  146.                 return name;
  147.             }
  148.         }
  149.  
  150.         public static class PartialMoney extends Partial implements Cost<MoneyDTO.Response.Partial> {
  151.             MoneyDTO.Response.Partial cost;
  152.  
  153.             protected PartialMoney() {
  154.             }
  155.  
  156.             public PartialMoney(ItemDTO.Types type, MoneyDTO.Response.Partial cost, String name, String visibleName) {
  157.                 super(type, name, visibleName);
  158.                 this.cost = cost;
  159.             }
  160.  
  161.             @Override
  162.             public MoneyDTO.Response.Partial getCost() {
  163.                 return cost;
  164.             }
  165.         }
  166.  
  167.         public static class PartialTrash extends Partial {
  168.  
  169.             protected PartialTrash() {
  170.             }
  171.  
  172.             public PartialTrash(ItemDTO.Types type, String name, String visibleName) {
  173.                 super(type, name, visibleName);
  174.             }
  175.         }
  176.  
  177.         public static abstract class Partial implements ItemDTO.Name, ItemDTO.Type, VisibleName {
  178.             ItemDTO.Types type;
  179.             String name;
  180.             String visibleName;
  181.  
  182.             protected Partial() {
  183.             }
  184.  
  185.             protected Partial(ItemDTO.Types type, String name, String visibleName) {
  186.                 this.type = type;
  187.                 this.name = name;
  188.                 this.visibleName = visibleName;
  189.             }
  190.  
  191.             @Override
  192.             public ItemDTO.Types getType() {
  193.                 return type;
  194.             }
  195.  
  196.             @Override
  197.             public String getName() {
  198.                 return name;
  199.             }
  200.  
  201.             @Override
  202.             public String getVisibleName() {
  203.                 return visibleName;
  204.             }
  205.         }
  206.  
  207.         public static class Id implements ItemDTO.Id {
  208.             Long id;
  209.  
  210.             protected Id() {
  211.             }
  212.  
  213.             public Id(Long id) {
  214.                 this.id = id;
  215.             }
  216.  
  217.             @Override
  218.             public Long getId() {
  219.                 return id;
  220.             }
  221.         }
  222.     }
  223. }
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement