4

I have an algorithm written in C and it has code which can be handled by C preprocessor but as there is no preprocessor in java i don't know how to write java code to handle such thing. The C code is

#ifdef Tile_size_utility
#define print_error 0
else
#define print_error 1
#endif

How can i implement this in Java?

Rog Matthews
  • 3,147
  • 16
  • 37
  • 56

5 Answers5

3

We can't translate it to Java. We have to analyze, where the property (Tile_size_utility) is set and where we need print_error. Then we can implement an equivalent solution.

Example: assuming, Tile_size_utility is a system property (set on the environment) and we need PRINT_ERROR as a (boolean) flag, then this should work:

public class MyClass {

  public final static boolean PRINT_ERROR = 
            (System.getProperty("Tile_size_utility") != null);

}

Now if you do some set Tile_size_utility=myutiliy or start java with the argument

-DTile_size_utility=myutiliy

then PRINT_ERROR will be set to true.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1

You can't.

But, Java does have inheritance and polymorphism. You could leverage these concepts along with the dependency injection pattern to solve the problem in a cleaner, but different manner.

I can give you a more exact answer if you post details about what that print_error does.

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
1
 Class ErrorVerbose { 
          private static boolean enabled = False;

          public static setEnable(boolean enable) 
                enabled = enable;
          }

          public static perror(String msg) {

               if (enabled) {
               /* Print */ 
               }

          }
   }


 class YourClass {

           public YourClass(....,boolean status) {

                ErrorVerbose.SetEnable(status)
                    .
                    . 
           }

   }

Well, yes, either you can make is a non static class so that other classes can enable / disable verbose. Advantage of making a ErrorVerbose class is that you add more info like time, date, function name (which called), etc which makes it more informative I just gave a skeleton.

alkber
  • 1,426
  • 2
  • 20
  • 26
0

Build a DefineManger using:

abstract public class DefineManager {
    abstract public boolean isDefined(String k);
    abstract public String  getDefine(String k);
    abstract public void    setDefine(String k, String v);
    abstract public void     undefine(String k);
}

if (isDefined("Tile_size_utility")) {
  setDefine("print_error", "0");
}
else {
  setDefine("print_error", "1");
}
Java42
  • 7,628
  • 1
  • 32
  • 50
0

The JVM compiles code to native dynamically so these settings can be changed at run time and get much the same performance.

You are likely to find that many of the tricks that C++ supports are not needed in Java as it has other solutions to the problem which are left to the JVM. Java tends to be feature poor compared to other languages, but what it does have is heavily optimised in ways which might surprise some developers.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130