HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Sorting a set of numbers in a file using Java BitSet

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortingfilenumbersjavausingbitsetset

Problem

I have implemented a program to sort a file consisting of numbers. The program generates a different output file with all numbers sorted in it. I have implemented this program using BitSet and want to understand BitSet.

```
public class Application {
public static void main(String[] args) throws Exception {

String inputFile = "c:\\temp\\numbers.txt";
NumberFileSorter.sort(inputFile, 100);
}
}

class NumberFileSorter {

private NumberFileSorter() {
}

public static void sort(String inputFile, int maxValue) throws Exception {

final BitSet bitSet = createBitSet(inputFile, maxValue);
final String outputFile = deriveOutputFile(inputFile);
writeBitSet(bitSet, outputFile);
}

private static BitSet createBitSet(String inputFile, int maxValue)
throws Exception {

final FileInputStream stream = new FileInputStream(inputFile);
final InputStreamReader streamReader = new InputStreamReader(stream);
final BufferedReader buffReader = new BufferedReader(streamReader);

String line = null;
int totalBits = maxValue + 1;
BitSet bitSet = new BitSet(totalBits);

try {
while ((line = buffReader.readLine()) != null) {
int number = Integer.parseInt(line);
bitSet.set(number, true);
}
} finally {
buffReader.close();
streamReader.close();
stream.close();
}

return bitSet;
}

private static String deriveOutputFile(String inputFile) {
String outputFileName = Paths.get(inputFile).getParent().toString()
+ "/output.txt";
return outputFileName;
}

private static void writeBitSet(BitSet bitSet, String outputFile)
throws Exception {

final File file = new File(outputFile);
file.createNewFile();

final FileOutputStream outputStream = new FileOutputStream(file);

Solution

Overall your code is very well formatted and readable but I have some complaints at the moment:

-
Don't declare all methods with throws Exception. Only declare the Exceptions you really need to declare, such as IOException. Declaring throws Exception will force the caller to also catch all RuntimeExceptions, which I say is a big no-no as that could potentially catch important bugs (NPEs for example) in your code. I prefer to let RuntimeExceptions cascade up the hierarchy, without catching them.

-
Your deriveOutputFile is as far as I can see only called from one place, and consists of only one line. Does it really need to be it's own method? I'd also say that it's name is somewhat ambiguous. deriveOutputFileName would be better.

-
Your entire class is static. I'm not so sure about this decision. If you'd want to test it in any way you would probably want it non-static, as you then could use a mocking framework to simulate the file input.

Regarding your questions I have some comments:

-
There's a really good question on StackOverflow about Testing internal implementation or public behaviour

-
Decoupling the sorting from the file system sounds like a very good idea and would make your code re-usable, so that you could easily use it in another project if needed. By all means, if you really want to then you can abstract things more by making the sorting an interface, and possibly the file system an interface. Remember that there's a risk of over-engineering though. Always remember the rule of YAGNIT.

Context

StackExchange Code Review Q#40855, answer score: 10

Revisions (0)

No revisions yet.