BAEL-3486 (#8458)
* Hexagonal architecture: a quick and practical example * BAEL-3486 commit * Formatting issue solved * Update code for consistency with article
This commit is contained in:
parent
e11c779a6c
commit
63397e4daa
|
@ -15,7 +15,7 @@ public class GreedyAlgorithm {
|
||||||
this.fp = new FollowersPath();
|
this.fp = new FollowersPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long findMostFollowersPath(String account) throws Exception {
|
public long findMostFollowersPath(String account) {
|
||||||
long max = 0;
|
long max = 0;
|
||||||
SocialUser toFollow = null;
|
SocialUser toFollow = null;
|
||||||
|
|
||||||
|
@ -31,12 +31,8 @@ public class GreedyAlgorithm {
|
||||||
if (currentLevel < maxLevel - 1) {
|
if (currentLevel < maxLevel - 1) {
|
||||||
currentLevel++;
|
currentLevel++;
|
||||||
max += findMostFollowersPath(toFollow.getUsername());
|
max += findMostFollowersPath(toFollow.getUsername());
|
||||||
//fp.addFollower(toFollow.getUsername(), max);
|
|
||||||
//fp.addCount(max);
|
|
||||||
return max;
|
return max;
|
||||||
} else {
|
} else {
|
||||||
//fp.addFollower(toFollow.getUsername(), max);
|
|
||||||
//fp.addCount(max);
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,7 @@ public class NonGreedyAlgorithm {
|
||||||
this.currentLevel = level;
|
this.currentLevel = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long findMostFollowersPath(String account) {
|
||||||
public long findMostFollowersPath(String account) throws Exception {
|
|
||||||
List<SocialUser> followers = tc.getFollowers(account);
|
List<SocialUser> followers = tc.getFollowers(account);
|
||||||
long total = currentLevel > 0 ? followers.size() : 0;
|
long total = currentLevel > 0 ? followers.size() : 0;
|
||||||
|
|
||||||
|
|
|
@ -20,9 +20,9 @@ public class SocialConnector {
|
||||||
return this.isCounterEnabled;
|
return this.isCounterEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SocialUser> getFollowers(String account) throws Exception {
|
public List<SocialUser> getFollowers(String account) {
|
||||||
if (counter < 0)
|
if (counter < 0)
|
||||||
throw new Exception ("API limit reached");
|
throw new IllegalStateException ("API limit reached");
|
||||||
else {
|
else {
|
||||||
if(this.isCounterEnabled) counter--;
|
if(this.isCounterEnabled) counter--;
|
||||||
for(SocialUser user : users) {
|
for(SocialUser user : users) {
|
||||||
|
|
|
@ -35,21 +35,21 @@ public class GreedyAlgorithmUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void greedyAlgorithmTest() throws Exception {
|
public void greedyAlgorithmTest() {
|
||||||
GreedyAlgorithm ga = new GreedyAlgorithm(prepareNetwork());
|
GreedyAlgorithm ga = new GreedyAlgorithm(prepareNetwork());
|
||||||
assertEquals(ga.findMostFollowersPath("root"), 5);
|
assertEquals(ga.findMostFollowersPath("root"), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nongreedyAlgorithmTest() throws Exception {
|
public void nongreedyAlgorithmTest() {
|
||||||
NonGreedyAlgorithm nga = new NonGreedyAlgorithm(prepareNetwork(), 0);
|
NonGreedyAlgorithm nga = new NonGreedyAlgorithm(prepareNetwork(), 0);
|
||||||
Assertions.assertThrows(Exception.class, () -> {
|
Assertions.assertThrows(IllegalStateException.class, () -> {
|
||||||
nga.findMostFollowersPath("root");
|
nga.findMostFollowersPath("root");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nongreedyAlgorithmUnboundedTest() throws Exception {
|
public void nongreedyAlgorithmUnboundedTest() {
|
||||||
SocialConnector sc = prepareNetwork();
|
SocialConnector sc = prepareNetwork();
|
||||||
sc.switchCounter();
|
sc.switchCounter();
|
||||||
NonGreedyAlgorithm nga = new NonGreedyAlgorithm(sc, 0);
|
NonGreedyAlgorithm nga = new NonGreedyAlgorithm(sc, 0);
|
||||||
|
|
Loading…
Reference in New Issue