Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CHALLENGE (work in pairs):
- // Improve code snippets listed below –
- //
- // 1 - represent Xml class in the system
- class XmlMarshaller {
- JaxbMarshaller jaxbMarshaller = new JaxbMarshaller(Invoice.class);
- byte[] marshallToXml(Invoice invoice) {
- try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
- return jaxbMarshaller.marshallObject(invoice, outStream).toByteArray();
- }
- }
- }
- class Xml<T> {
- byte[] bytes(T entity) {
- JaxbMarshaller jaxb = new JaxbMarshaller(entity.getClass());
- try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
- return jaxbMarshaller.marshallObject(invoice, outStream).toByteArray();
- }
- }
- }
- // 2 - refactor to iliminate all null checks and branching
- class AmazonS3File {
- private final String path;
- private Optional<Metadata> metadata = Optioal.EMPTY;
- void attach(Metadata metadata) {
- this.metadata = Optional.of(metadata);
- }
- Optional<Metadata> metadata() {…}
- }
- class AmazonS3Bucket {
- void upload(AmazonS3File file) {
- … upload ...
- file.metadata.ifPresent(uploadMetadata(file.getMetadata())); //this::uploadMetadata
- }
- }
- // 3 - refactor to conform to command-query separation (remember principle – "do or die")
- class Registration {
- private final RegistrationForm form;
- Optional<ValidationErrors> complete() {
- if (form.isValid()) {
- … complete registration ...
- return Optional.empty();
- } else {
- return form.validationErrors();
- }
- }
- }
- // 4 - does the method violate the principle of least astonishment? is there something suspicious with the design? why?
- // Ban ban(User user) {
- // Ban ban = new Ban(user.ipAddress(), Period.ofDays(3));
- // user.banned(); // sets "banned" boolean in User to "true"
- // return ban;
- // }
- User u = new User;
- u.ban(Period.ofDays(3)); // returns void
- // 5 – any ideas how to improve this code?
- void sort() {
- Collection<User> users = ...
- users.sort((one, another) -> Long.compare(one.getId(), another.getId()));
- }
- Comparator.comparing(User::getId)!!!!!!!!!!!!!!!!!!!
- // 6 – every raw() invocation leads to remote call (via Vault). Please fix
- class SecurePassword {
- private final Vault vault;
- private SecurePassword(Vault vault) {
- this.vault = vault;
- }
- public String raw() {
- return vault.verySecurePassword();
- }
- }
- class SecurePassword {
- private final Supplier<String> rawPAssword
- private final Vault vault;
- private SecurePassword(Vault vault) {
- //guava
- rawPassword = Suppliers.memoize(vault::verySecurePassword)
- this.vault = vault;
- }
- public String raw() {
- return rawPAssword.get();
- }
- }
- // 7 - implement "fullName" method, so that it returned "firstName lastName" if nickname is missing
- // or "firstName <nickname> lastName" if nickname is present.
- // for example – "Robert Martin" or "Robert <Uncle Bob> Martin"
- // don't optimize prematurely (e.g. prefer simple String concatenation over StringBuilder).
- class User {
- private final Optional<String> nickName;
- private final String firstName;
- private final String lastName;
- String fullName() {
- StringFormatter("%s %s%s", firstName, nickName.orElse(" "), lastName)
- }
- }
Add Comment
Please, Sign In to add comment