Java: implementing simple commands

I created a pkg for my usual simple commands. They are non-static in order to be more extensible, but somewhat more time consuming to use due to the creation of an object to use it. My other classes use them.

$ ls  *.java
CpF.java      IsBinary.java      RmLn.java        Tools.java
F2S.java      IsRoot.java        SaveToDisk.java  WordCount.java
Filters.java  ModRelativePaths.java  SetWord.java     WordNumber.java
Find.java     powerTools.java        Sort.java

      

Which option would you choose to simplify?

  • to create a small interface like "powerTools.java" for pkg.
  • to create a class with static methods for them.
  • add static method for each class
  • to stop using "create too many files" and centralize some?
  • add more?

Static Object VS Discussion

  1. How can you make external methods work like they do in your classes without referencing their class name? Not class_name.method (), just method (). External methods are taken from separate pkgs.

Code doesn't work with Import-static with my class and Pkg

$ cat Test.java 
import static a.*;

public class Test
{
    public static void main(String[] args)
    {
        a.printHello();
    }
}
$ cat a/Hello.java 
import java.io.*;

public class Hello
{
    public static printHello(){System.out.println("Hello");}
}

$ javac Test.java 
Test.java:1: cannot find symbol
symbol: class a
import static a.*;
              ^
Test.java:7: cannot find symbol
symbol  : variable a
location: class Test
        a.printHello();
        ^
2 errors
$ sed -i 's@a.printHello@printHello@g' Test.java 
$ javac Test.java 
Test.java:1: cannot find symbol
symbol: class a
import static a.*;
              ^
Test.java:7: cannot find symbol
symbol  : method printHello()
location: class Test
        printHello();

      

+2


a source to share


4 answers


At first glance, I'm pretty sure none of them maintain any state and therefore can be safely declared static

. I also get the impression that each class has only one public method that only performs one task at a time. It also looks like they are tied to a specific platform.

If all of the above is true, then you have 2 options:

  • Just group them all in a class public final class PowerTools

    with a constructor private

    (so it can't be instantiated) and public static

    each one doing the desired task. Ultimately, you can use the original class name as the method name.

  • Determine public interface PowerTools

    that you can implement LinuxPowerTools

    , WindowsPowerTools

    , MacPowerTools

    etc. accordingly and create an abstract PowerToolsFactory

    one to automatically return the desired implementation based on the current platform (you can use a system property os.name

    to sniff the current platform).

If it's for pure hobby purposes, option 1. But option 2 is more "professional" if you know what I mean.

Update : As per your update, the operator can be used for this import static

. Also see this Sun Tutorial .



eg.

import static java.lang.System.*;

public class Test {
    public static void main(String... args) {
        out.println("foo");
        exit(-1);
    }
}

      

If you have a class com.example.PowerTools

containing methods public static

, you can import it like this:

import static com.example.PowerTools.*;

public class Test {
    public static void main(String... args) {
        if (isBinary(file)) {
            // ...
        }
        saveToDisk(file);
    }
}

      

+3


a source


I am defining one class, G, which only has static methods. So I call G.print G.sort G.find etc.



0


a source


I would suggest that you bundle some of these features together into one class. Then expose your API via static methods, or just simply:

boolean isRoot = new DirectoryHelper().isRoot(someFile);
boolean isBinary = new DirectoryHelper().isBinary(someFile);
new DirectoryHelper().saveToDisk(someFile);

      

Yes, you create new objects every time, but if you do these operations tens of thousands of times, then this overhead will never be noticed. And with the above method, you now have a class that is easier to test and easier to perform dependency injection.

0


a source


If these are command line tools meant to be used from the shell, put all the classes in one JAR and write a short script to call each by name. Store scripts on your way eg. ~/bin

...

#!/bin/sh
java -cp your.jar pkg.Find "${@}"

#!/bin/sh
java -cp your.jar pkg.Sort "${@}"

      

etc .. "${@}"

send any parameters.

0


a source







All Articles