Skip to content

Commit e601df4

Browse files
committed
Renames the HashingNameGenerator to TinyNameGenerator, and reimplements the hashing version
using a consistent MD5 hash.
1 parent 8de8c9f commit e601df4

4 files changed

Lines changed: 61 additions & 6 deletions

File tree

Documentation.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@
117117
//
118118
// The `DefaultNameGenerator` implementation will leave names
119119
// as they are. A more compact form can be achieved by using
120-
// the `HashedNameGenerator`, which will try to shorten names
121-
// as much as possible.
120+
// the `TinyNameGenerator`, which will try to shorten names
121+
// as much as possible. For more consistent naming the
122+
// `HashedNameGenerator` can be used, which makes use of the
123+
// MD5 hashing algorithm.
122124
.useCustomNameGenerator(NameGenerator generator)
123125

124126
// Disable the printing of timestamps in the generated source

flapi-runtime/src/main/java/unquietcode/tools/flapi/runtime/BlockInvocationHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private Object invokeAndReturn(Method method, Object[] originalArgs, Object prox
106106

107107
private Object invokeAndReturn(Method method, Object[] originalArgs, Object proxy, MethodInfo info, ChainInfo[] chain) {
108108
// Don't use info.chainInfo() directly, since they might not match due
109-
// to legacy considerations (will be removed in 1.0).
109+
// to legacy considerations (will be removed in 1.0). TODO remove in 1.0
110110

111111
final int depth = chain.length;
112112

src/main/java/unquietcode/tools/flapi/generator/naming/HashedNameGenerator.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,29 @@
1717
package unquietcode.tools.flapi.generator.naming;
1818

1919
import com.google.common.base.Function;
20+
import com.google.common.hash.Hashing;
21+
22+
import java.nio.charset.Charset;
2023

2124
/**
22-
* Name generator implementation which shortens names into
23-
* smaller strings.
25+
* Name generator implementation which shortens method names into
26+
* smaller strings, and then hashes the entire class name using
27+
* an MD5 hasher. This has the added advantage that, since it is a
28+
* consistent hash, if there are no differences between names then
29+
* the hash will always be the same.
2430
*
2531
* @author Ben Fagin
2632
* @version 2015-01-14
2733
*/
2834
public class HashedNameGenerator extends DefaultNameGenerator {
2935
private final Function<String, String> methodNameHasher = new Hasher("m");
30-
private final Function<String, String> stateNameHasher = new Hasher("S");
36+
37+
private final Function<String, String> stateNameHasher = new Function<String, String>() {
38+
public @Override String apply(String string) {
39+
String hashed = Hashing.md5().hashString(string, Charset.forName("UTF-8")).toString();
40+
return "S"+hashed;
41+
}
42+
};
3143

3244
@Override
3345
public String methodName(String methodKey) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*********************************************************************
2+
Copyright 2015 the Flapi authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
********************************************************************/
16+
17+
package unquietcode.tools.flapi.generator.naming;
18+
19+
import com.google.common.base.Function;
20+
21+
/**
22+
* Name generator implementation which shortens names into
23+
* smaller strings.
24+
*
25+
* @author Ben Fagin
26+
* @version 2015-01-14
27+
*/
28+
public class TinyNameGenerator extends DefaultNameGenerator {
29+
private final Function<String, String> methodNameHasher = new Hasher("m");
30+
private final Function<String, String> stateNameHasher = new Hasher("S");
31+
32+
@Override
33+
public String methodName(String methodKey) {
34+
return methodNameHasher.apply(methodKey);
35+
}
36+
37+
@Override
38+
public String className(String stateKey) {
39+
return stateNameHasher.apply(stateKey);
40+
}
41+
}

0 commit comments

Comments
 (0)