Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored changes for seperated-interface #2881

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,67 @@
*/
public abstract class AbstractDocument implements Document {

private final Map<String, Object> properties;
private final Map<String, Object> documentProperties;

protected AbstractDocument(Map<String, Object> properties) {
Objects.requireNonNull(properties, "properties map is required");
this.properties = properties;
this.documentProperties = properties;
}

@Override
public Void put(String key, Object value) {
properties.put(key, value);
documentProperties.put(key, value);
return null;
}

@Override
public Object get(String key) {
return properties.get(key);
return documentProperties.get(key);
}

@Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> childConstructor) {
return Stream.ofNullable(get(key))
.filter(Objects::nonNull)
.map(el -> (List<Map<String, Object>>) el)
.findAny()
.stream()
.flatMap(Collection::stream)
.map(constructor);
.filter(Objects::nonNull)
.map(el -> (List<Map<String, Object>>) el)
.findAny()
.stream()
.flatMap(Collection::stream)
.map(childConstructor);
}

@Override
public String toString() {
return buildStringRepresentation();
}

private String buildStringRepresentation() {
var builder = new StringBuilder();
builder.append(getClass().getName()).append("[");
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value)
.append("]"));

// Explaining variable for document properties map
Map<String, Object> documentProperties = this.documentProperties;

// Explaining variable for the size of document properties map
int numProperties = documentProperties.size();

// Explaining variable for tracking the current property index
int currentPropertyIndex = 0;

// Iterate over document properties map
for (Map.Entry<String, Object> entry : documentProperties.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();

// Append key-value pair
builder.append("[").append(key).append(" : ").append(value).append("]");

// Add comma if not last property
if (++currentPropertyIndex < numProperties) {
builder.append(", ");
}
}

builder.append("]");
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,46 @@ void shouldIncludePropsInToString() {
assertTrue(document.toString().contains(VALUE));
}

@Test
void shouldHandleExceptionDuringConstruction() {
Map<String, Object> invalidProperties = null; // Invalid properties, causing NullPointerException

// Throw null pointer exception
assertThrows(NullPointerException.class, () -> {
// Attempt to construct a document with invalid properties
new DocumentImplementation(invalidProperties);
});
}

@Test
void shouldPutAndGetNestedDocument() {
// Creating a nested document
DocumentImplementation nestedDocument = new DocumentImplementation(new HashMap<>());
nestedDocument.put("nestedKey", "nestedValue");


document.put("nested", nestedDocument);

// Retrieving the nested document
DocumentImplementation retrievedNestedDocument = (DocumentImplementation) document.get("nested");

assertNotNull(retrievedNestedDocument);
assertEquals("nestedValue", retrievedNestedDocument.get("nestedKey"));
}

@Test
void shouldUpdateExistingValue() {
// Arrange
final String key = "key";
final String originalValue = "originalValue";
final String updatedValue = "updatedValue";

document.put(key, originalValue);

// Updating the value
document.put(key, updatedValue);

//Verifying that the updated value is retrieved correctly
assertEquals(updatedValue, document.get(key));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,54 @@
*/
package com.iluwatar.acyclicvisitor;

/**
* //Modem abstract class.
* converted to an interface
*/
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.acyclicvisitor;

/**
* //Modem abstract class.
* converted to an interface
*/
public interface Modem {
void accept(ModemVisitor modemVisitor);
}

public interface ZoomVisitor extends ModemVisitor {
void visit(Zoom zoom);
}

public class Zoom implements Modem {
@Override
public void accept(ModemVisitor modemVisitor) {
modemVisitor.visit(this);
}

// Other methods...
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,61 @@
*/
package com.iluwatar.acyclicvisitor;

/**
* ModemVisitor interface does not contain any visit methods so that it does not depend on the
* visited hierarchy. Each derivative's visit method is declared in its own visitor interface
*/
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.acyclicvisitor;

/**
* ModemVisitor interface does not contain any visit methods so that it does not depend on the
* visited hierarchy. Each derivative's visit method is declared in its own visitor interface
*/
public interface ModemVisitor {
// Visitor is a degenerate base class for all visitors.
void visit(Hayes hayes);
void visit(Zoom zoom);
}

public class ConfigureForDosVisitor implements ModemVisitor {
@Override
public void visit(Hayes hayes) {
LOGGER.info(hayes + " used with Dos configurator.");
}

@Override
public void visit(Zoom zoom) {
LOGGER.info(zoom + " used with Dos configurator.");
}
}

public class ConfigureForUnixVisitor implements ModemVisitor {
@Override
public void visit(Zoom zoom) {
LOGGER.info(zoom + " used with Unix configurator.");
}
}

2 changes: 1 addition & 1 deletion adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).

The MIT License
Copyright © 2014-2023 Ilkka Seppälä
Copyright © 2014-2022 Ilkka Seppälä

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.iluwatar.separatedinterface.taxes;

import com.iluwatar.separatedinterface.invoice.TaxCalculator;

public abstract class AbstractTaxCalculator implements TaxCalculator {
protected static final double TAX_PERCENTAGE = 0.0;

public abstract double calculate(double amount);

// Pull up this method
public double getTaxPercentage() {
return TAX_PERCENTAGE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@

import com.iluwatar.separatedinterface.invoice.TaxCalculator;

import static com.iluwatar.separatedinterface.taxes.AbstractTaxCalculator.TAX_PERCENTAGE;
import com.iluwatar.separatedinterface.taxes.AbstractTaxCalculator.*;

/**
* TaxCalculator for Domestic goods with 20% tax.
*/
public class DomesticTaxCalculator implements TaxCalculator {
public class DomesticTaxCalculator extends AbstractTaxCalculator implements TaxCalculator {

public static final double TAX_PERCENTAGE = 20;
//public static final double TAX_PERCENTAGE = 20;

@Override
public double calculate(double amount) {
double percent = getTaxPercentage();
return amount * TAX_PERCENTAGE / 100.0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
package com.iluwatar.separatedinterface.taxes;

import com.iluwatar.separatedinterface.invoice.TaxCalculator;
import static com.iluwatar.separatedinterface.taxes.AbstractTaxCalculator.TAX_PERCENTAGE;

/**
* TaxCalculator for foreign goods with 60% tax.
*/
public class ForeignTaxCalculator implements TaxCalculator {

public static final double TAX_PERCENTAGE = 60;

@Override
public double calculate(double amount) {

return amount * TAX_PERCENTAGE / 100.0;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.singleton;

/**
Expand Down