In Java, How to return a string or double depending on the circumstances of a method? Is it possible?

For example, I have a method that scans a string for data separated by a specified delimiter, but some elements may be names, and other elements may be numbers.

If a user calls my method to return the number of an X element from a delimited list, I want it to return a string if the X element is a name, or double if the X element is a number.

For example, it objectName.get(5);will receive the 5th element in the splitting list.

Should I use some type of overload for this?

Or would I have to do something like objectName.getDouble(5);and objectName.getString(5);based on the fact that the user knows what element 5 is?

But what if the user does not know what paragraph 5 is? He just needs a string or double, depending on what happens.

+3
source share
4 answers

Here is one way to do this:

public Object get() {
    if (blueMoon) {
        return new Double(42.0);
    } else {
        return "fred";
    }
}

Note that this will return the shell Double, not Double.

I don’t think this is a good idea, because as the caller must check the type of the return value and do a type cast in order to do something with it.

For the record, Java does not allow the method to return Stringor Double, because these types do not have a common supertype in a system such as Java.

+7
source

- Maybe/Option . :

public abstract class DoubleOrString 
{
    // Constraint isDouble() xor isString()
    public boolean isDouble();
    public boolean isString();

    //Must throw iff !isString()
    public String getString();

    //Must throw iff !ifDouble()
    public Double getDouble();

    public static DoubleOrString wrap(final double wrapMe)
    {
       return new DoubleOrString()
       {
             public boolean isDouble() {return true;}
             public boolean isString() {return false;}
             public Double getDouble() {return wrapMe;}
             public String getString() {throw new RuntimeException();}
       };
    }

    //same for wrap(String)
}

, , . get(), , (, ) , ,

objectName.get(5).getString();

get (int), String double, return

DoubleOrString.wrap(theThingToReturn)

, .

( - )

public static DoubleOrString parseADoubleOrString(String input) {
    try {
        return DoubleOrString.wrap(Integer.parseInt(input))
    } catch (NumberFormatException nfe) {
        return DoubleOrString.wrap(input);
    }
}

  String input = //get the input from the user somehow
  DoubleOrString parsed = parseADoubleOrString(input);
  if (parsed.isDouble())
      aFunctionThatTakesADouble(parsed.getDouble());
  else
      aFunctionThatTakesAString(parsed.getString());
+6

, . String, , , .

, , API, :

public class ValueExtractor {

    public ValueExtractor(String delimitedText) {
        // ...
    }

    /**
     * Determines whether there is a next element
     * to be returned
     */
    public boolean next() {
        // ...
    }

    public String get() {
        // ...
    }

    /**
     * Returns the value as a Double if possible
     * null otherwise.
     */
    public Double getPossibleDouble() {
        // ...
    }
}
+2

Java . ( , Java, JVM/-.)

, Java. Either<String,Double> ( , Object, C , DoubleOrString, B. Bear), , Java , getString(...) getDouble(...).

0

Source: https://habr.com/ru/post/1769844/


All Articles