Skip to content

Instantly share code, notes, and snippets.

@trvswgnr
Last active August 27, 2023 23:08
Show Gist options
  • Save trvswgnr/b8d6ea74f70332d5fe135844bf5560c2 to your computer and use it in GitHub Desktop.
Save trvswgnr/b8d6ea74f70332d5fe135844bf5560c2 to your computer and use it in GitHub Desktop.
left-pad for enterprise
interface Logger {
void error(String message, Throwable t);
}
class ConsoleLogger implements Logger {
private final String className;
ConsoleLogger(String className) {
this.className = className;
}
@Override
public void error(String message, Throwable t) {
System.err.println("ERROR: " + className + ": " + message);
t.printStackTrace(System.err);
}
}
interface PaddingService {
String pad(String input, int length, char padChar);
}
class LeftPaddingService implements PaddingService {
private static final Logger logger = new ConsoleLogger(LeftPaddingService.class.getName());
@Override
public String pad(String input, int length, char padChar) {
StringBuffer sb = new StringBuffer();
try {
while (sb.length() < length - input.length()) {
sb.append(padChar);
}
sb.append(input);
} catch (Exception e) {
logger.error("Error occurred while padding", e);
throw e;
}
return sb.toString();
}
}
enum PaddingStrategy {
LEFT,
}
interface PaddingServiceFactory {
PaddingService createPaddingService(PaddingStrategy strategy);
}
class PrimaryPaddingServiceFactory implements PaddingServiceFactory {
@Override
public PaddingService createPaddingService(PaddingStrategy strategy) {
switch (strategy) {
case LEFT:
return new LeftPaddingService();
default:
throw new IllegalArgumentException("Invalid padding strategy: " + strategy);
}
}
}
interface PaddingServiceAdapter {
String pad(String input, int length, char padChar);
}
class PrimaryPaddingServiceAdapter implements PaddingServiceAdapter {
private final PaddingService paddingService;
PrimaryPaddingServiceAdapter(PaddingService paddingService) {
this.paddingService = paddingService;
}
@Override
public String pad(String input, int length, char padChar) {
if (input.length() >= length) {
return input;
}
return paddingService.pad(input, length, padChar);
}
}
interface PresentationService {
void display(String data);
}
class ConsolePresentationService implements PresentationService {
@Override
public void display(String data) {
System.out.println(data);
}
}
class LeftPad {
private final PaddingServiceAdapter paddingServiceAdapter;
LeftPad(PaddingServiceAdapter paddingServiceAdapter) {
this.paddingServiceAdapter = paddingServiceAdapter;
}
String padString(String input, int length, char padChar) {
return paddingServiceAdapter.pad(input, length, padChar);
}
}
public class Main {
public static void main(String[] args) {
PaddingServiceFactory paddingServiceFactory = new PrimaryPaddingServiceFactory();
PaddingService paddingService = paddingServiceFactory.createPaddingService(PaddingStrategy.LEFT);
PaddingServiceAdapter paddingServiceAdapter = new PrimaryPaddingServiceAdapter(paddingService);
PresentationService presentationService = new ConsolePresentationService();
LeftPad leftPad = new LeftPad(paddingServiceAdapter);
String paddedString = leftPad.padString("tr4vvyr00lz", 20, '@');
presentationService.display(paddedString);
}
}
@WarrenBuffering
Copy link

lmfaooooo

@marcusziade
Copy link

This refactor offends me

@amanzrx4
Copy link

Damn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment