From 000ac071776cb30cc8c954211ab71802f010d7af Mon Sep 17 00:00:00 2001 From: mguarnaccia Date: Fri, 27 Dec 2019 18:20:10 +0100 Subject: [PATCH] BAEL-3486 (#8424) * Hexagonal architecture: a quick and practical example * BAEL-3486 commit * Formatting issue solved --- .../baeldung/algorithms/greedy/Follower.java | 20 +++++++ .../algorithms/greedy/FollowersPath.java | 44 ++++++++++++++ .../algorithms/greedy/GreedyAlgorithm.java | 47 +++++++++++++++ .../algorithms/greedy/NonGreedyAlgorithm.java | 44 ++++++++++++++ .../algorithms/greedy/SocialConnector.java | 36 ++++++++++++ .../algorithms/greedy/SocialUser.java | 38 ++++++++++++ .../greedy/GreedyAlgorithmUnitTest.java | 58 +++++++++++++++++++ 7 files changed, 287 insertions(+) create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/Follower.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/FollowersPath.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/GreedyAlgorithm.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/NonGreedyAlgorithm.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialConnector.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialUser.java create mode 100644 algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/greedy/GreedyAlgorithmUnitTest.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/Follower.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/Follower.java new file mode 100644 index 0000000000..e142f10eb4 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/Follower.java @@ -0,0 +1,20 @@ +package com.baeldung.algorithms.greedy; + +import lombok.Getter; + +public class Follower { + + @Getter String username; + @Getter long count; + + public Follower(String username, long count) { + super(); + this.username = username; + this.count = count; + } + + @Override + public String toString() { + return "User: " + username + ", Followers: " + count + "\n\r" ; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/FollowersPath.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/FollowersPath.java new file mode 100644 index 0000000000..2319c14af8 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/FollowersPath.java @@ -0,0 +1,44 @@ +package com.baeldung.algorithms.greedy; + +import java.util.ArrayList; +import java.util.List; + +public class FollowersPath { + + private List accounts; + private long count; + + public FollowersPath() { + super(); + this.accounts = new ArrayList<>(); + } + + public List getAccounts() { + return accounts; + } + public long getCount() { + return count; + } + + public void addFollower(String username, long count) { + accounts.add(new Follower(username, count)); + } + + public void addCount(long count) { + this.count += count; + } + + @Override + public String toString() { + String details = ""; + for(Follower a : accounts) { + details+=a.toString() + ", "; + } + + return "Total: " + count + ", \n\r" + + " Details: { " + "\n\r" + + details + "\n\r" + + " }"; + } + +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/GreedyAlgorithm.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/GreedyAlgorithm.java new file mode 100644 index 0000000000..d32ffb01b9 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/GreedyAlgorithm.java @@ -0,0 +1,47 @@ +package com.baeldung.algorithms.greedy; + +import java.util.List; + +public class GreedyAlgorithm { + + int currentLevel = 0; + final int maxLevel = 3; + SocialConnector sc; + FollowersPath fp; + + public GreedyAlgorithm(SocialConnector sc) { + super(); + this.sc = sc; + this.fp = new FollowersPath(); + } + + public long findMostFollowersPath(String account) throws Exception { + long max = 0; + SocialUser toFollow = null; + + List followers = sc.getFollowers(account); + for (SocialUser el : followers) { + long followersCount = el.getFollowersCount(); + if (followersCount > max) { + toFollow = el; + max = followersCount; + } + } + + if (currentLevel < maxLevel - 1) { + currentLevel++; + max += findMostFollowersPath(toFollow.getUsername()); + //fp.addFollower(toFollow.getUsername(), max); + //fp.addCount(max); + return max; + } else { + //fp.addFollower(toFollow.getUsername(), max); + //fp.addCount(max); + return max; + } + } + + public FollowersPath getFollowers() { + return fp; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/NonGreedyAlgorithm.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/NonGreedyAlgorithm.java new file mode 100644 index 0000000000..cb3d69a18e --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/NonGreedyAlgorithm.java @@ -0,0 +1,44 @@ +package com.baeldung.algorithms.greedy; + +import java.util.List; + +public class NonGreedyAlgorithm { + + int currentLevel = 0; + final int maxLevel = 3; + SocialConnector tc; + + public NonGreedyAlgorithm(SocialConnector tc, int level) { + super(); + this.tc = tc; + this.currentLevel = level; + } + + + public long findMostFollowersPath(String account) throws Exception { + List followers = tc.getFollowers(account); + long total = currentLevel > 0 ? followers.size() : 0; + + if (currentLevel < maxLevel ) { + currentLevel++; + + long[] count = new long[followers.size()]; + int i = 0; + for (SocialUser el : followers) { + NonGreedyAlgorithm sub = new NonGreedyAlgorithm(tc, currentLevel); + count[i] = sub.findMostFollowersPath(el.getUsername()); + i++; + } + + long max = 0; + for (; i > 0; i--) { + if (count[i-1] > max ) + max = count[i-1]; + } + + return total + max; + } + + return total; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialConnector.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialConnector.java new file mode 100644 index 0000000000..b8bbbdcfff --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialConnector.java @@ -0,0 +1,36 @@ +package com.baeldung.algorithms.greedy; + +import java.util.ArrayList; +import java.util.List; + +import lombok.Getter; +import lombok.Setter; + +public class SocialConnector { + private boolean isCounterEnabled = true; + private int counter = 4; + @Getter @Setter List users; + + public SocialConnector() { + users = new ArrayList<>(); + } + + public boolean switchCounter() { + this.isCounterEnabled = !this.isCounterEnabled; + return this.isCounterEnabled; + } + + public List getFollowers(String account) throws Exception { + if (counter < 0) + throw new Exception ("API limit reached"); + else { + if(this.isCounterEnabled) counter--; + for(SocialUser user : users) { + if (user.getUsername().equals(account)) { + return user.getFollowers(); + } + } + } + return new ArrayList<>(); + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialUser.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialUser.java new file mode 100644 index 0000000000..bc8679da79 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/greedy/SocialUser.java @@ -0,0 +1,38 @@ +package com.baeldung.algorithms.greedy; + +import java.util.ArrayList; +import java.util.List; + +import lombok.Getter; + +public class SocialUser { + + @Getter private String username; + @Getter private List followers; + + public SocialUser(String username) { + super(); + this.username = username; + this.followers = new ArrayList<>(); + } + + public SocialUser(String username, List followers) { + super(); + this.username = username; + this.followers = followers; + } + + public long getFollowersCount() { + return followers.size(); + } + + public void addFollowers(List followers) { + this.followers.addAll(followers); + } + + @Override + public boolean equals(Object obj) { + return ((SocialUser) obj).getUsername().equals(username); + } + +} diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/greedy/GreedyAlgorithmUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/greedy/GreedyAlgorithmUnitTest.java new file mode 100644 index 0000000000..173e3f8de5 --- /dev/null +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/greedy/GreedyAlgorithmUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung.algorithms.greedy; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class GreedyAlgorithmUnitTest { + + private SocialConnector prepareNetwork() { + SocialConnector sc = new SocialConnector(); + SocialUser root = new SocialUser("root"); + SocialUser child1 = new SocialUser("child1"); + SocialUser child2 = new SocialUser("child2"); + SocialUser child3 = new SocialUser("child3"); + SocialUser child21 = new SocialUser("child21"); + SocialUser child211 = new SocialUser("child211"); + SocialUser child2111 = new SocialUser("child2111"); + SocialUser child31 = new SocialUser("child31"); + SocialUser child311 = new SocialUser("child311"); + SocialUser child3111 = new SocialUser("child3111"); + child211.addFollowers(Arrays.asList(new SocialUser[]{child2111})); + child311.addFollowers(Arrays.asList(new SocialUser[]{child3111})); + child21.addFollowers(Arrays.asList(new SocialUser[]{child211})); + child31.addFollowers(Arrays.asList(new SocialUser[]{child311, + new SocialUser("child312"), new SocialUser("child313"), new SocialUser("child314")})); + child1.addFollowers(Arrays.asList(new SocialUser[]{new SocialUser("child11"), new SocialUser("child12")})); + child2.addFollowers(Arrays.asList(new SocialUser[]{child21, new SocialUser("child22"), new SocialUser("child23")})); + child3.addFollowers(Arrays.asList(new SocialUser[]{child31})); + root.addFollowers(Arrays.asList(new SocialUser[]{child1, child2, child3})); + sc.setUsers(Arrays.asList(new SocialUser[]{root, child1, child2, child3, child21, child31, child311, child211})); + return sc; + } + + @Test + public void greedyAlgorithmTest() throws Exception { + GreedyAlgorithm ga = new GreedyAlgorithm(prepareNetwork()); + assertEquals(ga.findMostFollowersPath("root"), 5); + } + + @Test + public void nongreedyAlgorithmTest() throws Exception { + NonGreedyAlgorithm nga = new NonGreedyAlgorithm(prepareNetwork(), 0); + Assertions.assertThrows(Exception.class, () -> { + nga.findMostFollowersPath("root"); + }); + } + + @Test + public void nongreedyAlgorithmUnboundedTest() throws Exception { + SocialConnector sc = prepareNetwork(); + sc.switchCounter(); + NonGreedyAlgorithm nga = new NonGreedyAlgorithm(sc, 0); + assertEquals(nga.findMostFollowersPath("root"), 6); + } +} \ No newline at end of file