vaadin / framework

Vaadin 6, 7, 8 is a Java framework for modern Java web applications.
http://vaadin.com/
Other
1.78k stars 730 forks source link

VaadinIcons fromCodepoint static method is missing #9199

Open dyorgio opened 7 years ago

dyorgio commented 7 years ago

Vaadin 8.x.x is awesome!

I'm currently migrating a 7.7.8 application to 8.0.5.

An important method present in FontAwsome enum is

public static FontAwesome fromCodepoint(final int codepoint){...}

It doesn't exist on VaadinIcons enum class.

It is very useful if you store icon information on database by codepoint int.

Some hints:

rntrp commented 7 years ago

No need for switch/case statements. Vaadin Icons codepoint space begins with 0xE600, and all icons are lying close together. One cound simply create a static enum array for all icons with array index being codepoint - 0xE600. Your proposed method would be something like this:

public static FontAwesome fromCodepoint(final int codepoint) {
  return STATIC_ARRAY_WITH_ALL_THE_ENUMS[codepoint - 0xE600];
}
dyorgio commented 7 years ago

Nice idea @rntrp

Final version (using VaadinIcons enum static array):

    public static VaadinIcons fromCodepoint(final int codepoint) {
        return VaadinIcons.values()[codepoint - 0xE600];
    }

EDIT: Invalid option, VaadinIcons.values() are ordered alphabetically, not by codepoint.

rntrp commented 7 years ago

I'm afraid it won't work this way. Since VaadinIcons enum entries are declared in the alphabetic order, VaadinIcons.values() will hence return values sorted alphabetically, not by their respective codepoint. This means, you'd have to declare a separate array like this:

private static final VaadinIcons[] CODEPOINT_MAP = new VaadinIcons[values().length];
static {
  for (VaadinIcons icon : values())
    CODEPOINT_MAP[icon.codepoint - 0xE600] = icon;
}

/** @throws ArrayIndexOutOfBoundsException for illegal codepoints */
public static VaadinIcons fromCodepoint(final int codepoint) {
  return CODEPOINT_MAP[codepoint - 0xE600];
}
stale[bot] commented 6 years ago

Hello there!

It looks like this issue hasn't progressed lately. There are so many issues that we just can't deal them all within a reasonable timeframe.

There are a couple of things you could help to get things rolling on this issue (this is an automated message, so expect that some of these are already in use):

Thanks again for your contributions! Even though we haven't been able to get this issue fixed, we hope you to report your findings and enhancement ideas in the future too!

Peppe commented 6 years ago

This is just pseudocode written inline but you should be able to loop the VaadinIcons values and find the one with the correct codepoint that way:

public static VaadinIcons findByCodepoint(int codepoint){
    for(VaadinIcons icon : values()){
        if( icon.getCodepoint().equals(codepoint)){
            return icon;
        }
    }
    return null;
}

If you need to do many lookups, you could consider a static Map<int, VaadinIcons > and do the standard map lookup against that. Inline code, again:

  private static final Map<int, VaadinIcons> vaadinIconLookup = new HashMap<int, VaadinIcons>();

  static {
    for (VaadinIcons icon : VaadinIcons.values()) {
      vaadinIconLookup.put(icon.getCodepoint(), icon);
    }
  }

  public static Day get(String abbreviation) {
    return vaadinIconLookup.get(abbreviation);
  }