Java

Member

adelia_boyer

Some interesting source code from Android 1.5 🤨

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
if (f.mColor != null) {
  int c = -1;


  if (f.mColor.equalsIgnoreCase("aqua")) {
      c = 0x00FFFF;
  } else if (f.mColor.equalsIgnoreCase("black")) {
      c = 0x000000;
  } else if (f.mColor.equalsIgnoreCase("blue")) {
      c = 0x0000FF;
  } else if (f.mColor.equalsIgnoreCase("fuchsia")) {
      c = 0xFF00FF;
  } else if (f.mColor.equalsIgnoreCase("green")) {
      c = 0x008000;
  } else if (f.mColor.equalsIgnoreCase("grey")) {
      c = 0x808080;
  } else if (f.mColor.equalsIgnoreCase("lime")) {
      c = 0x00FF00;
  } else if (f.mColor.equalsIgnoreCase("maroon")) {
      c = 0x800000;
  } else if (f.mColor.equalsIgnoreCase("navy")) {
      c = 0x000080;
  } else if (f.mColor.equalsIgnoreCase("olive")) {
      c = 0x808000;
  } else if (f.mColor.equalsIgnoreCase("purple")) {
      c = 0x800080;
  } else if (f.mColor.equalsIgnoreCase("red")) {
      c = 0xFF0000;
  } else if (f.mColor.equalsIgnoreCase("silver")) {
      c = 0xC0C0C0;
  } else if (f.mColor.equalsIgnoreCase("teal")) {
      c = 0x008080;
  } else if (f.mColor.equalsIgnoreCase("white")) {
      c = 0xFFFFFF;
  } else if (f.mColor.equalsIgnoreCase("yellow")) {
      c = 0xFFFF00;
  } else {
      try {
          c = XmlUtils.convertValueToInt(f.mColor, -1);
      } catch (NumberFormatException nfe) {
          // Can't understand the color, so just drop it.
      }
  }


Member

zora.stokes

by zora.stokes , in category: Java , 16 days ago
no answers

Thread join implementation in Java

1
2
3
4
5
6
7
try {
     while (sender.isAlive()) {
         this.sleep(100);//static field usage(!)
     }
} catch (java.lang.InterruptedException e) {
     Log.log(Log.ERROR, this, e);
}


Member

zora.stokes

by zora.stokes , in category: Java , 17 days ago
no answers

How to return boolean result in Java 🤗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/**
 * Delete. * 
 * @param entity the entity 
 * @return true, if successful 
 */ 
public boolean delete(T entity) {
 	boolean deleted=false; 
	getSession().delete(entity); 
	flush(); 
	//when no hibernate runtime exception has arised...set to true.
	deleted = true;
	return deleted;
}


Member

letha

Parse Command method in real project on the Java 😑

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
private String parseCommand(final String value) {
    final StringBuilder s = new StringBuilder();
    for (int i = 0; i < value.length(); i++) {
      final char c = value.charAt(i);
      switch (c) {
      case 'a':
        s.append("[A]");
        break;
      case 'b':
        s.append("[B]");
        break;
      case 'c':
        s.append("[C]");
        break;
      case 'x':
        s.append("[X]");
        break;
      case 'y':
        s.append("[Y]");
        break;
      case 'z':
        s.append("[Z]");
        break;
      case 'B':
        s.append("<Back>");
        break;
      case 'F':
        s.append("<Forward>");
        break;
      case 'D':
        s.append("<Down>");
        break;
      case 'U':
        s.append("<Up>");
        break;
      case '/':
        s.append(" HOLD ");
        break;
      case '~':
        s.append(" RELEASE ");
        break;
      case '$':
        s.append(" INMOVE ");
        break;
      case '+':
        s.append(" AND ");
        break;
      case '>':
        s.append(" START ");
        break;
      default:
        s.append(c);
      }
    }
    return s.toString();
  }
}


Member

adelia_boyer

by adelia_boyer , in category: Java , 22 days ago
no answers

Found in real Java project 😂

1
2
3
4
5
6
try {
	throw new Exception("TRANSACTION IS OPENED");
} catch (Exception e) {
	System.out.println(e.getMessage());
	e.printStackTrace();
}


Member

zora.stokes

How to get difference between dates in Java 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public static long getDifference(Date date1, Date date2, int unit) {
	if (date1 == null || date2 == null) {
		throw new IllegalArgumentException("Date cannot be null!");
	}

	if (date1.after(date2)) {
		Date swap = date1;
		date1 = date2;
		date2 = swap;
	}
		
	long result = 0;
	
	long diff = date2.getTime() - date1.getTime() + (60 * 60 * 1000L);
		
	switch (unit) {
	case Calendar.YEAR:
		result = diff / (60 * 60 * 24 * 365 * 1000L);
		break;
	case Calendar.MONTH:
		result = diff / (60 * 60 * 24 * 30 * 1000L);
		break;
	case Calendar.DAY_OF_YEAR:
		result = diff / (60 * 60 * 24 * 1000L);
		break;
	default:
		throw new UnsupportedOperationException("Impossible to get time difference in coerced unit.");
	}
		
	return result;
}


Member

zora.stokes

by zora.stokes , in category: Java , a month ago
no answers

How to debug in JAVA 😝

1
2
3
4
5
if(false || DEBUG) 
{
   System.out.print("analysis: "+Integer.toBinaryString(analysis));
   System.out.println(", "+WalkerFactory.getAnalysisString(analysis));
}

Member

lou_lindgren

by lou_lindgren , in category: Java , 2 months ago
no answers

Convert Boolean into Int in Java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public static int booleanToInt(Boolean value) {
   int res;
   if (value == null) {
      res = 0;
   
   } else {
      if (value) {
         res = 1;
   } else {
         res = 0;
      }
   }
   
   return res;
}


Member

adelia_boyer

by adelia_boyer , in category: Java , 2 months ago
no answers

Add items to list one by one in Java

1
2
3
4
5
6
List list = new ArrayList();
list.add(".jpg");
list.add(".png");
list.add(".gif");
list.add(".jpeg");
list.add(".bmp");

Member

mittie.gottlieb

by mittie.gottlieb , in category: Java , 2 months ago
no answers

Beautiful check empty string in Java 😐

1
2
3
4
5
6
7
public Book(Integer id, String name, String author, BigDecimal price, Integer sales, Integer stock, String imgPath) {
   ...
   this.stock = stock;
   if (imgPath != null && !"".equals(imgPath)) {
      this.imgPath = imgPath;
   }
}