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

Une version java, simple. #10

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

*.iml
out/**
.idea/**
13 changes: 13 additions & 0 deletions Choix-implem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

Une version de plus en java.

Choix d'implémentation
======================

Sont privilégés la concision, la lisibilité et la performance. Pas besoin de sortir toute l'artillerie lourde pour
le moment.

Tant que le besoin fonctionnel n'évolue pas, les 2 champs StringBuilder et int me permettent de simplifier les
signatures de méthodes. La contrepartie est que je ne teste pas le code méthode par méthode.


79 changes: 79 additions & 0 deletions src/FooBarQix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.util.HashMap;
import java.util.Map;

public class FooBarQix {


public static void main(String[] args) {
for (int i = 1; i < 100; i++) {
System.out.println(new FooBarQix(i).getReplacementLine());
}
}

private final int input;
private StringBuilder replacementLine;

public FooBarQix(int toAnalyse) {
input = toAnalyse;
}

public String getReplacementLine() {
replacementLine = new StringBuilder();
appendNamesByDivisibles();
appendNamesByContent();
appendNumberIfNoName();
return replacementLine.toString();
}

private void appendNamesByDivisibles() {
for (FBQEnum fbqEnum : FBQEnum.values()) {
if (isDivisibleBy(fbqEnum)) {
appendName(fbqEnum);
}
}
}

private boolean isDivisibleBy(FBQEnum fbqEnum) {
return input % fbqEnum.value == 0;
}

private void appendNamesByContent() {
for (char c : String.valueOf(input).toCharArray()) {
appendNameIfExistsReplacementFor(c);
}
}

private void appendNameIfExistsReplacementFor(char c) {
FBQEnum fbqEnum = ST_VALUE_TO_FBQ.get(String.valueOf(c));
if (fbqEnum != null)
appendName(fbqEnum);
}

private void appendName(FBQEnum fbqEnum) {
replacementLine.append(fbqEnum.name());
}

private void appendNumberIfNoName() {
if (replacementLine.length() == 0)
replacementLine.append(input);
}

private static enum FBQEnum {
Foo(3), Bar(5), Qix(7);

private int value;

FBQEnum(int value) {
assert value < 10;
this.value = value;
}
}

private static final Map<String, FBQEnum> ST_VALUE_TO_FBQ = new HashMap<String, FBQEnum>();
static {
for (FBQEnum fbqEnum : FBQEnum.values()) {
ST_VALUE_TO_FBQ.put(String.valueOf(fbqEnum.value), fbqEnum);
}
}

}
36 changes: 36 additions & 0 deletions src/TestFooBarQuix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
*/
public class TestFooBarQuix {

@Test
public void highLevelTests(){
assertIntToFooBarQix(1 , "1");
assertIntToFooBarQix(2 , "2");
assertIntToFooBarQix(3 , "FooFoo");
assertIntToFooBarQix(6 , "Foo");
assertIntToFooBarQix(13 , "Foo");
assertIntToFooBarQix(15 , "FooBarBar");
assertIntToFooBarQix(33 , "FooFooFoo");
assertIntToFooBarQix(21 , "FooQix");
assertIntToFooBarQix(51 , "FooBar");
assertIntToFooBarQix(53 , "BarFoo");
}

private void assertIntToFooBarQix(int i, String fooBar) {
assertEquals(new FooBarQix(i).getReplacementLine(), fooBar);
}

@Test
public void perf(){
long startTime = System.currentTimeMillis();
for (int i = 1; i < 1000001; i++) {
new FooBarQix(i).getReplacementLine();
}
System.out.println("1 000 000 en " + (System.currentTimeMillis() - startTime) + " ms.");
}

}