Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Enum' - 12 code snippet(s) found

 Sample 1. Switch Case with Enum
Admin
info@buggybread.com
public enum State {
NEVADA, CALIFORNIA, WASHINGTON;
}

public static void method(States stateName) {
switch (stateName) {
case NEVADA:
System.out.println("You are in Nevada");
break;

case CALIFORNIA:
System.out.println("You are in California");
break;

case WASHINGTON:
System.out.println("You are in Washington");
break;
}
}

   Like      Feedback     switch case  switch  case  enum   switch with enums   switch and enums


 Sample 2. Enum with a method to get the constant having specific member element.

public enum JavaFramework {
SPRING("Spring","Web"),
STRUTS("Struts","Web"),
JSF("JSF","View"),
CRUNCH("Apache Crunch","BigData");


public String name;
public String type;

JavaFramework(String name, String type){
this.name = name;
this.type = type;
}

static public List<JavaFramework> getByType(String type) {
List<JavaFramework> frameworks = new ArrayList();
for(JavaFramework framework: JavaFramework.values()){
if(framework.type.equalsIgnoreCase(type)){
frameworks.add(framework);
}
}

return frameworks;
}
}

   Like      Feedback     enum  java.util.list


 Sample 3. Initialize maps through static block

public class CoinChanger {
   private static Map<Currency, Integer> cashBox;
   private static Map<Currency, Integer> change;

enum Currency {
      DOLLAR,QUARTER,DIME,NICKEL,PENNY;
   }

   static {
      cashBox = new TreeMap<Currency, Integer>();
      change = new TreeMap<Currency, Integer>();
      initializeCashBox();
   }

   private static void initializeCashBox(){
      //set the cash box
      cashBox.put(Currency.DOLLAR, 50);
      cashBox.put(Currency.QUARTER, 0);
      cashBox.put(Currency.DIME, 50);
      cashBox.put(Currency.NICKEL, 50);
      cashBox.put(Currency.PENNY, 50);
   }
}
}

   Like      Feedback     static block   enum   initializing maps   initializing treemap


 Sample 4. Enum implementing TemporalField

public enum MyChronoField implements TemporalField {

NANO_OF_SECOND("NanoOfSecond", NANOS, SECONDS, ValueRange.of(12l,45l));


private final String name;
private final TemporalUnit baseUnit;
private final TemporalUnit rangeUnit;
private final ValueRange range;
private final String displayNameKey;

private MyChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
this.name = name;
this.baseUnit = baseUnit;
this.rangeUnit = rangeUnit;
this.range = range;
this.displayNameKey = null;
}

private MyChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,
ValueRange range, String displayNameKey) {
this.name = name;
this.baseUnit = baseUnit;
this.rangeUnit = rangeUnit;
this.range = range;
this.displayNameKey = displayNameKey;
}
}

   Like      Feedback     enum   TemporalField  java.time.temporal   java.timejava.time.temporal.TemporalField


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Internal Implementation of MinguoEra

public enum MinguoEra implements Era {
BEFORE_ROC,
ROC;

public static MinguoEra of(int minguoEra) {
switch (minguoEra) {
case 0:
return BEFORE_ROC;
case 1:
return ROC;
default:
throw new DateTimeException("Invalid era: " + minguoEra);
}
}

@Override
public int getValue() {
return ordinal();
}

}

   Like      Feedback     MinguoEra  java.time  java.time.chrono  internal implementation  enum  switch  throw  exception throw


 Sample 6. Initialize member elements ( maps ) using constructor

public class CoinChanger {
   private static Map<Currency, Integer> cashBox;
   private static Map<Currency, Integer> change;

   enum Currency {
      DOLLAR,QUARTER,DIME,NICKEL,PENNY;
   }

   CoinChanger() {
      cashBox = new TreeMap<Currency, Integer>();
      change = new TreeMap<Currency, Integer>();
      initializeCashBox();
   }

   private static void initializeCashBox(){
      //set the cash box
      cashBox.put(Currency.DOLLAR, 50);
      cashBox.put(Currency.QUARTER, 0);
      cashBox.put(Currency.DIME, 50);
      cashBox.put(Currency.NICKEL, 50);
      cashBox.put(Currency.PENNY, 50);
   }
}
}

   Like      Feedback     constructor   initializing maps   initializing treemap   enum


 Sample 7. Usage of EnumMap

Map<Country,String> map = new HashMap();
map.put(Country.US, "Washington");
map.put(Country.CANADA, "Ottawa");
map.put(Country.CANADA, "Ottawa");
map.put(Country.SOUTH_AFRICA, "Pretoria");
map.put(Country.SOUTH_AFRICA, "Cape Town");
map.put(Country.SOUTH_AFRICA, "Bloemfontein");

Map<String,String> enumMap = new EnumMap(map); // Prints {CANADA=Ottawa, US=Washington, SOUTH_AFRICA=Bloemfontein}

System.out.println(enumMap); // Duplicates not allowed as it's a Map, Sorted as per order of Enum Declarations

   Like      Feedback     EnumMap  Collections framework  Map  Map with enum keys


 Sample 8. Code Sample / Example / Snippet of org.apache.calcite.adapter.enumerable.PhysType

  public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

PhysType physType =

PhysTypeImpl.of(

implementor.getTypeFactory(),

getRowType(),

pref.preferArray());



if (table instanceof JsonTable) {

return implementor.result(

physType,

Blocks.toBlock(

Expressions.call(table.getExpression(JsonTable.class),

"enumerable")));

}

return implementor.result(

physType,

Blocks.toBlock(

Expressions.call(table.getExpression(CsvTranslatableTable.class),

"project", Expressions.constant(fields))));

}


   Like      Feedback      org.apache.calcite.adapter.enumerable.PhysType


 Sample 9. Code Sample / Example / Snippet of org.apache.calcite.adapter.enumerable.EnumerableLimit

    public void onMatch(RelOptRuleCall call) {

final EnumerableLimit limit = call.rel(0);

final RelNode converted = convert(limit);

if (converted != null) {

call.transformTo(converted);

}

}


   Like      Feedback      org.apache.calcite.adapter.enumerable.EnumerableLimit


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 10. Code Sample / Example / Snippet of org.apache.bcel.generic.EnumElementValueGen

    public void testCreateEnumElementValue() throws Exception

{

final ClassGen cg = createClassGen("HelloWorld");

final ConstantPoolGen cp = cg.getConstantPool();

final ObjectType enumType = new ObjectType("SimpleEnum"); // Supports rainbow

final EnumElementValueGen evg = new EnumElementValueGen(enumType, "Red", cp);

assertTrue(

"The new ElementValue value index should match the contents of the constantpool but "

+ evg.getValueIndex() + "!=" + cp.lookupUtf8("Red"),

evg.getValueIndex() == cp.lookupUtf8("Red"));

checkSerialize(evg, cp);

}


   Like      Feedback      org.apache.bcel.generic.EnumElementValueGen


 Sample 11. Code Sample / Example / Snippet of org.apache.bcel.classfile.LineNumberTable

    public Attribute copy( final ConstantPool _constant_pool ) {

final LineNumberTable c = (LineNumberTable) clone();

c.line_number_table = new LineNumber[line_number_table.length];

for (int i = 0; i < line_number_table.length; i++) {

c.line_number_table[i] = line_number_table[i].copy();

}

c.setConstantPool(_constant_pool);

return c;

}


   Like      Feedback      org.apache.bcel.classfile.LineNumberTable


 Sample 12. Wait till Page loads in selenium / Web Driver

public void waitTillPageLoads() {

ExpectedCondition < Boolean > expectedCondition = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete");
try {
WebDriverWait wait = new WebDriverWait(driver, 90);
wait.until(expectedCondition);
driver.quit();
} catch (Exception exception) {

}
}

   Like      Feedback     selenum  webdriver



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner