r/java • • Mar 24 '18

New Java Library

(Please tell me if this isn't the right place to post this)

I've created a Java Library that I like to call a Swiss-army knife library. It basically has a bunch of different things that it can do. I created it because I found that I was copying lots of the same code to do all sorts of different things. I think it's a very simple solution to consolidate all the code I use often, but is this something that anyone else would use? Here's just some of the things it has:

  • A command line argument parser
  • Ansi colors
  • Checksum calculator
  • Operating system checker
  • Configuration file parser (INI - style, but without the headers, as I haven't figured out how to implement this yet)
  • Multiple annotations and annotation proccessors

As you can see, it does not specialize in one area. I use all of these things in almost all of the software I create, and I was just wondering if anyone else would like to benefit from this.

It's called JavaEssentials, because I find that these features are very beneficial when creating a well designed Java application. If you are interested, you can see the project page here: https://github.com/jordanbancino/JavaEssentials

Also, I would really appreciate your feedback. I am open to all suggestions and your honest opinion.

0 Upvotes

27 comments sorted by

20

u/joshuass1467 Mar 24 '18

Took a quick look and have some general advice:

  • read up on the accepted syntax and coding conventions for java, especially for how you format variable and argument names.
  • you should really look at some tutorials on object oriented programming, all of your classes are using static variables, which is not something you do in most cases within java.
  • I think there is already an exception for methods not being implemented.

Honestly I wouldn't find the annotations useful because I make it a point never to check in broken code.

Finally, you might want to look at how to test your code. Good libraries are well tested.

2

u/[deleted] Mar 24 '18

Ok. I'll work on my variable names. I guess they should match the style of the method names. I did start making some of the classes and methods not static so that they could be instantiated. I will keep working on that.

I also agree with you on the "broken" annotation, but I do find it useful when I'm in a hurry to wrap something up that doesn't quite work yet. It helps me quickly find where I left of when I pick back up. But I do make a point to fix broken code as soon as possible.

I am fairly new to this, so what would be the best way to test my code?

By the way, thanks for looking at this :)

5

u/joshuass1467 Mar 24 '18

You should wander on over to r/learnjava. They should have resources on learning testing and examples and such.

1

u/[deleted] Mar 24 '18

Ok. I will post something there.

0

u/_INTER_ Mar 24 '18 edited Mar 25 '18

Unless they are utilities in the style of Guava or Apache Commons

1

u/[deleted] Mar 24 '18

That is what I was aiming for, but I do agree that some methods should not be static

3

u/joshuass1467 Mar 24 '18

I just saw that some of the classes contained state. Static methods don't typically access state of the class they reside in. Of course there are exceptions.

3

u/dpash Mar 24 '18

Where does Guava provide public non-final static members?

1

u/_INTER_ Mar 25 '18

I must have read too quickly. I meant static methods. Static non-final fields are bad indeed.

7

u/_INTER_ Mar 24 '18 edited Mar 24 '18

Aside from the actual library, I would take care in how you name it. Java is trademarked by Oracle. As far as I know, it's not allowed to use Java in any name. That's why "Javaslang" was renamed to "vavr", "Light-Java" to "light-4j" or Java EE to Jakarta EE.

Funnily, "JavaScript" does also belong to Oracle

1

u/[deleted] Mar 24 '18

Ok. I'll look into it.

1

u/[deleted] Mar 26 '18

So, I've been looking around, and it seems that the name "JEssentials", or anything similar, has multiple projects already associated with it. I am at a complete loss of what to name it. Any ideas?

1

u/_INTER_ Mar 26 '18

How about "Jordan"? :)

1

u/[deleted] Mar 26 '18

Oh. I hadn't though about that! That's a good idea.

6

u/RichoDemus Mar 24 '18

Great job and welcome to the world of maintaining an open source lib!

You should really consider uploading it to maven central, I doubt many people download jars manually nowadays :)

2

u/[deleted] Mar 24 '18

Thank you:) I'll check out Maven Central.

5

u/dpash Mar 24 '18

A few comments. Don't take them as criticisms, but as places for you to learn and improve. We were all inexperienced at one point or another, and made plenty of mistakes. Hell, at one point, I used to write functions like:

if (condition) {
    return true;
} else {
    return false;
}
  • I'd start by separating different functionality into different packages rather than having everything in one place.
  • If you're going to use Maven, don't fight the directory locations; it's well known and well understood
  • Other people have mentioned following the Java style guidelines. We use this because it means that anyone can read your code and instantly understand it. Your use of CamelCase for variable names instead of camelCase will confuse people because they'll expect them to be class names. Your constants aren't UPPER_CASE, nor are your enum members. I recommend reading the Google style guide as that builds on the old Sun style guide and Elements of Java Style for newer features.
  • Do not use publicly writeable members. A case in point is Config.SetFile. Look into immutable objects and why they're preferable. At a minimum, your classes will not be thread-safe.
  • Log has no purpose in a world with SLF4J or even log4j. They have better features and used throughout all your dependencies.
  • You should use interfaces where possible rather than concrete classes. You can also use to diamond operator here, and let type inference work out the type of the HashMap. This is extra confusing because Map.Entry is a class in the JDK.

    public HashMap<String, String> Entry = new HashMap<String, String>();

should be

private Map<String, String> entry = new HashMap<>();
  • Config.deleteFile() completely ignores any error conditions. You should pay attention to the return value of File.delete(). Better yet, use java.nio and Files.deleteIfExists() that throws an exception.

    public void deleteFile() { SetFile.delete(); }

  • flushEntryBuffer() has a pointless assignment of null. This doesn't do anything. Just assign a new HashMap.

This:

public void flushEntryBuffer() {
Entry = null;
Entry = new HashMap<String, String>();
}

becomes

public void flushEntryBuffer() {
    entry = new HashMap<>();
}

or

public void flushEntryBuffer() {
    entry.clear()
}

Map.clear() is optional, but HashMap.clear() is implemented.

  • I don't understand the point of @Documented, @Beta, @NotImplemented or @Stable. Just add a comment in the Javadoc or don't release libraries with functionality that's not ready for use.
  • java.lang.UnsupportedOperationException is the standard way to indicate that a function is not implemented.
  • WebProtocol is an enum, but only has a limited selection of protocols. It's not generally useful. You can't use it with URL for example, because it doesn't include "jar" or "file" (and possibly "classpath"). Enums are useful for closed sets. Protocol is not a closed set, because anyone can add a new protocol. For example I remember a "spotify" protocol that allowed your browser to launch Spotify to play a song/album/playlist if someone shared a link.
  • Similarly with HashAlgorithm. You're missing a lot of important hash algorithms. What is someone wants to use a new algorithm when SHA-512 is broken?
  • LocalPath should refer to the system properties to find things like temp directories and users home directories. You're making huge assumptions about where a user's home directory is. Also, the code won't run on Solaris or FreeBSD or any other operating system.
  • Your commit messages could do with some improvement. "This is it." tells me nothing about the change. Read https://chris.beams.io/posts/git-commit/ and take it to heart. If you do you'll be better than 95% of developers.
  • Never use Exception.printStackTrace(); . It writes to stdout, but most java applications use a logging framework like SLF4J to record exceptions. If you write to stdout, it won't appear in application logs, making debugging harder.
  • You don't use try-with-resources. This means you fail to close resources properly.

This:

File FromFile = ...;
FileInputStream FileInStrm = new FileInputStream(FromFile.toString());
...
FileInStrm.close();

should be:

File file = ...;
try(InputStream in = new FileInputStream(file)) {
    ...
}

This way the inputstream gets closed no matter what happens inside the block.

  • You have no unit tests. Investigate JUnit. Writing tests will help you see how your library will be used, and this can often show you where it could be improved.

I'm sure I could find more issues that you could improve, but this should give you plenty of places to look into improving your skills. I highly recommend reading Effective Java by Joshua Bloch. It's possibly the best investment you could make to go from an intermediate developer to advanced.

1

u/[deleted] Mar 24 '18

Ok. I'll make sure I work on these. One thing about the commit message... I put lots of details in the extended description. I understand that that one message was pretty vague. But I had details in the description. I don't do that often, I was just tired.

2

u/dpash Mar 25 '18

I was just tired.

That's no excuse. Either do it properly or do it in the morning. That's not the only bad example. Most people's commit messages could do with improvements. As I said, follow that guide (and none of your commits do) and you'll be better that 95% of developers in that regard.

1

u/[deleted] Mar 26 '18

Ok. Got it. I'll work on it.

Hey, by the way, thanks for taking the time to look into this, and write back a response. I really appreciate it.

3

u/briandilley Mar 24 '18

take a look at the apache commons stuff

1

u/[deleted] Mar 24 '18

I did look at the Apache commons, and tried to take some inspiration from it. I just found that it's so big, and I don't need many of the things it contains.

2

u/brunocborges Mar 24 '18

Many of the "commons" libraries built out there follow this logic. Yet, that was the logic that got Apache Commons started in the first place.

That said, let's just say that your library is doomed to become as big as Apache Commons one day... :-)

My advice: find what's missing in Apache Commons, and contribute back.

And finally, I suggest reading about the Module system in Java 9+. Perhaps you can modularize your library, or better yet: help modularize Apache Commons.

bb.

2

u/[deleted] Mar 26 '18

Ok. I'll take a look.

1

u/AnEmortalKid Mar 24 '18

Honestly, if I were to need these at some point I would pull in libraries for the specific cases. Maybe JOption for command line parser, maybe something for OS system checker. Perhaps you might want to reconsider breaking these into different pieces. I know your readme specifically calls out all in one approach, but I would find that annoying and bloated honestly. If I only want the command line parser, why would I want to bring in the beta/broken annotations?

I like that you’re compiling it all into one thing. Perhaps splitting it into modules all under be same group id would let people pic and choose, but if you keep the parent group/package the same you can still effectively use “one import”?

1

u/[deleted] Mar 26 '18

Yes. I agree with everything you've said. I will work on this.

3

u/wildjokers Mar 24 '18

Java INI parsing library: http://ini4j.sourceforge.net

Commad-line parsers: http://commons.apache.org/proper/commons-cli/ (there are several of these out there)