Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A simple adapter class that converts between input and output POJOs
- * All classes are contained within this single file for simplicity
- */
- public class PojoAdapter {
- /**
- * Sample Input POJO representing user data from an external system
- */
- public static class InputPojo {
- private String firstName;
- private String lastName;
- private int age;
- private String emailAddress;
- private String phoneNumber;
- // Constructors
- public InputPojo() {}
- public InputPojo(String firstName, String lastName, int age, String emailAddress, String phoneNumber) {
- this.firstName = firstName;
- this.lastName = lastName;
- this.age = age;
- this.emailAddress = emailAddress;
- this.phoneNumber = phoneNumber;
- }
- // Getters and Setters
- public String getFirstName() { return firstName; }
- public void setFirstName(String firstName) { this.firstName = firstName; }
- public String getLastName() { return lastName; }
- public void setLastName(String lastName) { this.lastName = lastName; }
- public int getAge() { return age; }
- public void setAge(int age) { this.age = age; }
- public String getEmailAddress() { return emailAddress; }
- public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; }
- public String getPhoneNumber() { return phoneNumber; }
- public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }
- @Override
- public String toString() {
- return "InputPojo{" +
- "firstName='" + firstName + '\'' +
- ", lastName='" + lastName + '\'' +
- ", age=" + age +
- ", emailAddress='" + emailAddress + '\'' +
- ", phoneNumber='" + phoneNumber + '\'' +
- '}';
- }
- }
- /**
- * Sample Output POJO representing user data for internal system
- */
- public static class OutputPojo {
- private String fullName;
- private String ageCategory;
- private String email;
- private String contact;
- private boolean isAdult;
- // Constructors
- public OutputPojo() {}
- public OutputPojo(String fullName, String ageCategory, String email, String contact, boolean isAdult) {
- this.fullName = fullName;
- this.ageCategory = ageCategory;
- this.email = email;
- this.contact = contact;
- this.isAdult = isAdult;
- }
- // Getters and Setters
- public String getFullName() { return fullName; }
- public void setFullName(String fullName) { this.fullName = fullName; }
- public String getAgeCategory() { return ageCategory; }
- public void setAgeCategory(String ageCategory) { this.ageCategory = ageCategory; }
- public String getEmail() { return email; }
- public void setEmail(String email) { this.email = email; }
- public String getContact() { return contact; }
- public void setContact(String contact) { this.contact = contact; }
- public boolean isAdult() { return isAdult; }
- public void setAdult(boolean adult) { isAdult = adult; }
- @Override
- public String toString() {
- return "OutputPojo{" +
- "fullName='" + fullName + '\'' +
- ", ageCategory='" + ageCategory + '\'' +
- ", email='" + email + '\'' +
- ", contact='" + contact + '\'' +
- ", isAdult=" + isAdult +
- '}';
- }
- }
- /**
- * Adapter method that converts InputPojo to OutputPojo
- * @param input The input POJO to convert
- * @return The converted output POJO
- */
- public static OutputPojo adaptInputToOutput(InputPojo input) {
- if (input == null) {
- return null;
- }
- OutputPojo output = new OutputPojo();
- // Combine first and last name into full name
- output.setFullName(input.getFirstName() + " " + input.getLastName());
- // Categorize age
- output.setAgeCategory(categorizeAge(input.getAge()));
- // Map email address to email
- output.setEmail(input.getEmailAddress());
- // Map phone number to contact
- output.setContact(input.getPhoneNumber());
- // Determine if adult (18 or older)
- output.setAdult(input.getAge() >= 18);
- return output;
- }
- /**
- * Helper method to categorize age into groups
- * @param age The age to categorize
- * @return The age category as a string
- */
- private static String categorizeAge(int age) {
- if (age < 13) {
- return "Child";
- } else if (age < 18) {
- return "Teenager";
- } else if (age < 65) {
- return "Adult";
- } else {
- return "Senior";
- }
- }
- /**
- * Reverse adapter method that converts OutputPojo back to InputPojo
- * Note: Some data loss may occur due to different field structures
- * @param output The output POJO to convert
- * @return The converted input POJO
- */
- public static InputPojo adaptOutputToInput(OutputPojo output) {
- if (output == null) {
- return null;
- }
- InputPojo input = new InputPojo();
- // Split full name back to first and last name (simple approach)
- String[] nameParts = output.getFullName().split(" ", 2);
- input.setFirstName(nameParts.length > 0 ? nameParts[0] : "");
- input.setLastName(nameParts.length > 1 ? nameParts[1] : "");
- // Age cannot be precisely determined from category, so we'll use defaults
- input.setAge(getDefaultAgeForCategory(output.getAgeCategory()));
- // Map email back to email address
- input.setEmailAddress(output.getEmail());
- // Map contact back to phone number
- input.setPhoneNumber(output.getContact());
- return input;
- }
- /**
- * Helper method to get a default age for an age category
- * @param category The age category
- * @return A default age for that category
- */
- private static int getDefaultAgeForCategory(String category) {
- switch (category) {
- case "Child": return 10;
- case "Teenager": return 16;
- case "Adult": return 30;
- case "Senior": return 70;
- default: return 25;
- }
- }
- /**
- * Main method to demonstrate the adapter functionality
- */
- public static void main(String[] args) {
- // Create a sample input POJO
- System.out.println("Original Input POJO:");
- System.out.println(input);
- System.out.println();
- // Adapt input to output
- OutputPojo output = adaptInputToOutput(input);
- System.out.println("Adapted Output POJO:");
- System.out.println(output);
- System.out.println();
- // Adapt output back to input (demonstrating reverse adaptation)
- InputPojo reversedInput = adaptOutputToInput(output);
- System.out.println("Reverse Adapted Input POJO:");
- System.out.println(reversedInput);
- System.out.println();
- // Test with different age categories
- System.out.println("Testing different age categories:");
- System.out.println("Child: " + adaptInputToOutput(child));
- System.out.println("Teenager: " + adaptInputToOutput(teenager));
- System.out.println("Senior: " + adaptInputToOutput(senior));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment