1 package org.broadinstitute.hellbender.utils.recalibration; 2 3 public enum EventType { 4 BASE_SUBSTITUTION("M", "Base Substitution"), 5 BASE_INSERTION("I", "Base Insertion"), 6 BASE_DELETION("D", "Base Deletion"); 7 8 private final String representation; 9 private final String longRepresentation; 10 11 /** 12 * Returns a cached value of the EventType.values() call (more precisely - an unmodifiable list view of that array). 13 * 14 * Every call to EventType.values() (or any enum type) creates a new array instance but they are all equal (ie contain identical elements). 15 * This is very expensive and wasteful when this array is created billions of times as in the case of BQSR. 16 * 17 * The solution is to create it once and reuse. 18 * However, we can't expose this array in an API because we can't make an array immutable. 19 * Exposing this array as list also does not work because performance of Collections.UnmodifiableCollection.iterator() is very bad and ruins our performance. 20 * The solution is to expose this array via read only calls and have clients iterate explicitly. 21 */ 22 private static final EventType[] cachedValues = EventType.values(); 23 EventType(final String representation, final String longRepresentation)24 private EventType(final String representation, final String longRepresentation) { 25 this.representation = representation; 26 this.longRepresentation = longRepresentation; 27 } 28 29 /** 30 * Get the EventType corresponding to its ordinal index 31 * @param index an ordinal index 32 * @return the event type corresponding to ordinal index 33 */ eventFrom(final int index)34 public static EventType eventFrom(final int index) { 35 return cachedValues[index]; 36 } 37 38 /** 39 * Get the EventType with short string representation 40 * @throws IllegalArgumentException if representation doesn't correspond to one of EventType 41 * @param representation short string representation of the event 42 * @return an EventType 43 */ eventFrom(final String representation)44 public static EventType eventFrom(final String representation) { 45 for (EventType eventType : cachedValues) { 46 if (eventType.representation.equals(representation)) { 47 return eventType; 48 } 49 } 50 51 throw new IllegalArgumentException(String.format("Event %s does not exist.", representation)); 52 } 53 54 @Override toString()55 public String toString() { 56 return representation; 57 } 58 prettyPrint()59 public String prettyPrint() { 60 return longRepresentation; 61 } 62 }