shivam197

Adapter

Aug 5th, 2025 (edited)
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.38 KB | Software | 0 0
  1. /**
  2.  * A simple adapter class that converts between input and output POJOs
  3.  * All classes are contained within this single file for simplicity
  4.  */
  5. public class PojoAdapter {
  6.    
  7.     /**
  8.      * Sample Input POJO representing user data from an external system
  9.      */
  10.     public static class InputPojo {
  11.         private String firstName;
  12.         private String lastName;
  13.         private int age;
  14.         private String emailAddress;
  15.         private String phoneNumber;
  16.        
  17.         // Constructors
  18.         public InputPojo() {}
  19.        
  20.         public InputPojo(String firstName, String lastName, int age, String emailAddress, String phoneNumber) {
  21.             this.firstName = firstName;
  22.             this.lastName = lastName;
  23.             this.age = age;
  24.             this.emailAddress = emailAddress;
  25.             this.phoneNumber = phoneNumber;
  26.         }
  27.        
  28.         // Getters and Setters
  29.         public String getFirstName() { return firstName; }
  30.         public void setFirstName(String firstName) { this.firstName = firstName; }
  31.        
  32.         public String getLastName() { return lastName; }
  33.         public void setLastName(String lastName) { this.lastName = lastName; }
  34.        
  35.         public int getAge() { return age; }
  36.         public void setAge(int age) { this.age = age; }
  37.        
  38.         public String getEmailAddress() { return emailAddress; }
  39.         public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; }
  40.        
  41.         public String getPhoneNumber() { return phoneNumber; }
  42.         public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }
  43.        
  44.         @Override
  45.         public String toString() {
  46.             return "InputPojo{" +
  47.                     "firstName='" + firstName + '\'' +
  48.                     ", lastName='" + lastName + '\'' +
  49.                     ", age=" + age +
  50.                     ", emailAddress='" + emailAddress + '\'' +
  51.                     ", phoneNumber='" + phoneNumber + '\'' +
  52.                     '}';
  53.         }
  54.     }
  55.    
  56.     /**
  57.      * Sample Output POJO representing user data for internal system
  58.      */
  59.     public static class OutputPojo {
  60.         private String fullName;
  61.         private String ageCategory;
  62.         private String email;
  63.         private String contact;
  64.         private boolean isAdult;
  65.        
  66.         // Constructors
  67.         public OutputPojo() {}
  68.        
  69.         public OutputPojo(String fullName, String ageCategory, String email, String contact, boolean isAdult) {
  70.             this.fullName = fullName;
  71.             this.ageCategory = ageCategory;
  72.             this.email = email;
  73.             this.contact = contact;
  74.             this.isAdult = isAdult;
  75.         }
  76.        
  77.         // Getters and Setters
  78.         public String getFullName() { return fullName; }
  79.         public void setFullName(String fullName) { this.fullName = fullName; }
  80.        
  81.         public String getAgeCategory() { return ageCategory; }
  82.         public void setAgeCategory(String ageCategory) { this.ageCategory = ageCategory; }
  83.        
  84.         public String getEmail() { return email; }
  85.         public void setEmail(String email) { this.email = email; }
  86.        
  87.         public String getContact() { return contact; }
  88.         public void setContact(String contact) { this.contact = contact; }
  89.        
  90.         public boolean isAdult() { return isAdult; }
  91.         public void setAdult(boolean adult) { isAdult = adult; }
  92.        
  93.         @Override
  94.         public String toString() {
  95.             return "OutputPojo{" +
  96.                     "fullName='" + fullName + '\'' +
  97.                     ", ageCategory='" + ageCategory + '\'' +
  98.                     ", email='" + email + '\'' +
  99.                     ", contact='" + contact + '\'' +
  100.                     ", isAdult=" + isAdult +
  101.                     '}';
  102.         }
  103.     }
  104.    
  105.     /**
  106.      * Adapter method that converts InputPojo to OutputPojo
  107.      * @param input The input POJO to convert
  108.      * @return The converted output POJO
  109.      */
  110.     public static OutputPojo adaptInputToOutput(InputPojo input) {
  111.         if (input == null) {
  112.             return null;
  113.         }
  114.        
  115.         OutputPojo output = new OutputPojo();
  116.        
  117.         // Combine first and last name into full name
  118.         output.setFullName(input.getFirstName() + " " + input.getLastName());
  119.        
  120.         // Categorize age
  121.         output.setAgeCategory(categorizeAge(input.getAge()));
  122.        
  123.         // Map email address to email
  124.         output.setEmail(input.getEmailAddress());
  125.        
  126.         // Map phone number to contact
  127.         output.setContact(input.getPhoneNumber());
  128.        
  129.         // Determine if adult (18 or older)
  130.         output.setAdult(input.getAge() >= 18);
  131.        
  132.         return output;
  133.     }
  134.    
  135.     /**
  136.      * Helper method to categorize age into groups
  137.      * @param age The age to categorize
  138.      * @return The age category as a string
  139.      */
  140.     private static String categorizeAge(int age) {
  141.         if (age < 13) {
  142.             return "Child";
  143.         } else if (age < 18) {
  144.             return "Teenager";
  145.         } else if (age < 65) {
  146.             return "Adult";
  147.         } else {
  148.             return "Senior";
  149.         }
  150.     }
  151.    
  152.     /**
  153.      * Reverse adapter method that converts OutputPojo back to InputPojo
  154.      * Note: Some data loss may occur due to different field structures
  155.      * @param output The output POJO to convert
  156.      * @return The converted input POJO
  157.      */
  158.     public static InputPojo adaptOutputToInput(OutputPojo output) {
  159.         if (output == null) {
  160.             return null;
  161.         }
  162.        
  163.         InputPojo input = new InputPojo();
  164.        
  165.         // Split full name back to first and last name (simple approach)
  166.         String[] nameParts = output.getFullName().split(" ", 2);
  167.         input.setFirstName(nameParts.length > 0 ? nameParts[0] : "");
  168.         input.setLastName(nameParts.length > 1 ? nameParts[1] : "");
  169.        
  170.         // Age cannot be precisely determined from category, so we'll use defaults
  171.         input.setAge(getDefaultAgeForCategory(output.getAgeCategory()));
  172.        
  173.         // Map email back to email address
  174.         input.setEmailAddress(output.getEmail());
  175.        
  176.         // Map contact back to phone number
  177.         input.setPhoneNumber(output.getContact());
  178.        
  179.         return input;
  180.     }
  181.    
  182.     /**
  183.      * Helper method to get a default age for an age category
  184.      * @param category The age category
  185.      * @return A default age for that category
  186.      */
  187.     private static int getDefaultAgeForCategory(String category) {
  188.         switch (category) {
  189.             case "Child": return 10;
  190.             case "Teenager": return 16;
  191.             case "Adult": return 30;
  192.             case "Senior": return 70;
  193.             default: return 25;
  194.         }
  195.     }
  196.    
  197.     /**
  198.      * Main method to demonstrate the adapter functionality
  199.      */
  200.     public static void main(String[] args) {
  201.         // Create a sample input POJO
  202.         InputPojo input = new InputPojo("John", "Doe", 25, "[email protected]", "+1-555-123-4567");
  203.        
  204.         System.out.println("Original Input POJO:");
  205.         System.out.println(input);
  206.         System.out.println();
  207.        
  208.         // Adapt input to output
  209.         OutputPojo output = adaptInputToOutput(input);
  210.        
  211.         System.out.println("Adapted Output POJO:");
  212.         System.out.println(output);
  213.         System.out.println();
  214.        
  215.         // Adapt output back to input (demonstrating reverse adaptation)
  216.         InputPojo reversedInput = adaptOutputToInput(output);
  217.        
  218.         System.out.println("Reverse Adapted Input POJO:");
  219.         System.out.println(reversedInput);
  220.         System.out.println();
  221.        
  222.         // Test with different age categories
  223.         System.out.println("Testing different age categories:");
  224.        
  225.         InputPojo child = new InputPojo("Alice", "Smith", 8, "[email protected]", "555-0001");
  226.         System.out.println("Child: " + adaptInputToOutput(child));
  227.        
  228.         InputPojo teenager = new InputPojo("Bob", "Johnson", 16, "[email protected]", "555-0002");
  229.         System.out.println("Teenager: " + adaptInputToOutput(teenager));
  230.        
  231.         InputPojo senior = new InputPojo("Carol", "Williams", 70, "[email protected]", "555-0003");
  232.         System.out.println("Senior: " + adaptInputToOutput(senior));
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment