0

I need to modify the 3rd method mapDirectionToTrafficProfileDirection() to display Both. Direction class is an enum class and just has IN and OUT.

public enum Direction {
IN,
OUT;

and TrafficProfileExtension class also has enum for Direction Egress, Ingress, Both;

public class TrafficProfileExtension {
private TrafficProfile entity;
public static enum Direction {
    Egress, Ingress, Both;
}

I need to modify the 3rd method to dislay Both is there a way I can add it without changing/adding a new enum in Direction class?

private TrafficProfileExtension.Direction mapDirectionToTrafficProfileDirection (Direction direction){
    if (Direction.OUT.equals(direction)){
        return TrafficProfileExtension.Direction.Egress;
    }
    if (Direction.IN.equals(direction)){
        return TrafficProfileExtension.Direction.Ingress;
    }
    if (Direction. ??  .equals(direction)){
        return TrafficProfileExtension.Direction.Both;
    }
    return null;
}
    }

We have this method in our code and they were able to add the enum without changing the enum in Direction class. Example method below:

private  List<Direction> mapTrafficProfileDirection (TrafficProfileExtension.Direction direction)  throws  Exception  {

    List<Direction>  directionList = new ArrayList<Direction>();

    if (TrafficProfileExtension.Direction.Egress.equals(direction)){
        directionList.add(Direction.OUT);
        return directionList;
    }
    if (TrafficProfileExtension.Direction.Ingress.equals(direction)){
        directionList.add(Direction.IN);
        return directionList;
    }
    if (TrafficProfileExtension.Direction.Both.equals(direction)){
        directionList.add(Direction.OUT);
        directionList.add(Direction.IN);
        return directionList;

    }

    throw new Exception("Illegal direction passed through method " + direction.name());

}
6
  • So you want another enum value without creating a new enum value? I don't understand what you want to do. Commented Sep 15, 2015 at 15:41
  • 4
    The Direction enum will never be both IN and OUT. It can be IN, OUT, or null. Commented Sep 15, 2015 at 15:41
  • Well your direction right now obviously can't be "both" at the same time. Commented Sep 15, 2015 at 15:42
  • This seems to be a duplicate of this question: stackoverflow.com/questions/4960167/… Other answer says Set<Direction> can be used instead of C++ style "or" Commented Sep 15, 2015 at 15:42
  • 1
    Are you sure that you have to add the third option? If Direction is only used by objects that require just IN and OUT, and you just want to convert it into the second type, then you translate what you have. I'd say the problem would be translating the reverse. Commented Sep 15, 2015 at 15:51

3 Answers 3

3

You could call the method with null as a special case.

if (direction == null){
    return TrafficProfileExtension.Direction.Both;
}

That isn't a very clean solution though, as null would usually imply that there is "no direction".


So, it seems you are already using a list to map from Direction to TrafficProfileExtension.Direction, so why not use a list for mapping in the other direction too?

private TrafficProfileExtension.Direction mapDirectionToTrafficProfileDirection(List<Direction> directionList) {

    if(directionList.contains(Direction.IN) && directionList.contains(Direction.OUT)){
        return TrafficProfileExtension.Direction.Both;
    }
    if (directionList.contains(Direction.IN)){
        return TrafficProfileExtension.Direction.Ingress;
    }
    if (directionList.contains(Direction.OUT)){
        return TrafficProfileExtension.Direction.Egress;
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Notice that in your mapTrafficProfileDirection method you are returning a List<Direction> to be able to map TrafficProfileExtension.Direction to Direction. Therefore you will have to accept a List<Direction> to do the inverse operation. You can overload it to accept just a single Direction but in that method you will not have to worry about the case of both directions.

mapDirectionToTrafficProfileDirection (List<Direction> direction)

Comments

1

You would need to work with Set<Direction>:

public enum Direction {

    IN,
    OUT;

    public static final Set<Direction> BOTH = Collections.unmodifiableSet(EnumSet.allOf(Direction.class));
}

public static class TrafficProfileExtension {

    public static enum Direction {

        Egress, Ingress, Both;
    }

    private TrafficProfileExtension.Direction mapDirectionToTrafficProfileDirection(Set<Test.Direction> directions) {
        if (directions.containsAll(Test.Direction.BOTH)) {
            return Direction.Both;
        } else if (directions.contains(Test.Direction.IN)) {
            return Direction.Egress;
        } else if (directions.contains(Test.Direction.OUT)) {
            return Direction.Ingress;
        } else {
            // Neither!!! What do you want to do?
            return null;
        }
    }
}

2 Comments

I have an error when calling this method The method mapDirectionToTrafficProfileDirection(Set<Direction>) in the type CosRetrieve is not applicable for the arguments (Direction) I am calling this method: Direction direction = allocInfo.getDirection(); TrafficProfileExtension.Direction tpDirection = mapDirectionToTrafficProfileDirection(direction);
@Gio - I changed the signature to Set<Test.Direction>. Try EnumSet.of to make a Set<Enum>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.