Java - Sizuha/devdog GitHub Wiki
OS X์์ JDK ์ค์น๊ฒฝ๋ก(JAVA_HOME) ํ์ธํ๊ธฐ
$ /usr/libexec/java_home
๋จ์ํ ํํ๋ก ์ฌ์ฉํ๊ณ ์ ํ๋ค๋ฉด,
public enum ErrorCode { SERVER_CONNECT_ERR, LOGIN_WRONG }
๊ทธ๋ฌ๋ฐ, enum๋ ์ฌ์ค์ ํด๋์ค์ด๋ค. ๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑ์๋ ๋ฌผ๋ก ์ด๊ณ , ๋งค์๋๋ ํฌํจ ํ ์ ์๋ค.
public enum ErrorCode {
SERVER_CONNECT_ERR(400), LOGIN_WRONG(100);
private int value;
private ErrorCode(int value) { this.value = value; }
public int toInt() { return value; }
}
static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
for (T o : a) {
c.add(o); // Correct
}
}
public class Hoge<T> {
private T t;
}
public class A {}
public class B extends A {}
public class C extends B {}
public class Foo1<T extends B> {} // BใCใใใฎๆดพ็
// class&interface[&interface[...] ]
public class Bar<T extends B & java.io.Serializable> {}
ๅคๆฐใชใฉใฎๅใฎๅฎฃ่จๆใซใฏใฏใคใซใใซใผใใไฝฟใใใจใใงใใใ
Hoge<?> hoge = new Hoge<String>();
Hoge<? extends A> a = new Hoge<B>();
ใใฎใฏใคใซใใซใผใ?ใซใฏsuperใจextendsใงๅข็ใ่จญๅฎใใใใจใใงใใใ&ใซใใ่คๆฐใฎinterfaceใฎ็ถๆฟใฏ่กจ็พใงใใชใใ
String request_get(String url, List<NameValuePair> params) {
HttpClient httpclient = new DefaultHttpClient();
String content;
try {
if (params != null && params.size() > 0) {
boolean first = true;
String param_str = "?";
for (NameValuePair nv : params) {
param_str += (first ? "" : "&") + nv.getName() + "=" + nv.getValue();
first = false;
}
URLEncoder.encode(param_str, "UTF-8");
}
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
content = EntityUtils.toString(response.getEntity());
}
catch (IOException e) {
content = null;
}
return content;
}
String request_post(String url, List<NameValuePair> params) {
Response result = new Response();
String content;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
if (params != null && params.size() > 0) {
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
content = EntityUtils.toString(response.getEntity());
}
catch (ClientProtocolException e) {
content = null;
}
catch (IOException e) {
content = null;
}
return content;
}
์ฐธ๊ณ ๋งํฌ
- http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
- http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html
(Argument List) -> body
(int x, int y) -> x + y
() -> 42
(String s) -> { System.out.println(s); } // return์ด ์๋ ๊ฒฝ์ฐ.
//์ธ์์ ํ์
์ ์๋ตํ ์๋ ์๋ค.
(x, y) -> x + y
s -> { System.out.println(s); }
https://qiita.com/takumi-n/items/369dd3fcb9ccb8fcfa44
- ใณใฌใฏใทใงใณใใstreamใๅๅพ
- streamใซๅฏพใใฆๆบ่ถณใใใพใงใไธญ้ๆไฝใใๅฎ่กใใณใฌใฏใทใงใณใฎไธญ่บซใ้ฝๅใใๅคๆ
- ใ็ต็ซฏๆไฝใใงๅคๆใใใณใฌใฏใทใงใณใฎไธญ่บซใซๅฏพใใฆๅฆ็ใ้ฉ็จใใ
http://stackoverflow.com/questions/2462398/how-to-get-the-ip-address-from-the-domain-name-in-java
InetAddress.getByName("xxxx.yyy.com").getHostAddress();
// for Android
public void selectFragment (MainMenu mainMenu){
int position = mainMenus.indexOf(mainMenu);
onNavigationDrawerItemSelected(position);
setTitle(mainMenus.get(position).getName());
Fragment fragment = null;
try {
Class<?> fragmentClass = Class.forName(mainMenus.get(position).getClassName());
fragment = (Fragment) fragmentClass.newInstance();
replaceFragment(fragment);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (java.lang.InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* bytesToHex method
* Found on the internet
* http://stackoverflow.com/a/9855338
*/
static final char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public final static boolean isValidFileName(String filename_without_path) {
final String[] reservedChars = {"|", "\\", "?", "*", "<", "\"", ":", ">"};
if (filename_without_path == null || filename_without_path.trim().isEmpty()) return false;
for (String c: reservedChars){
if (filename_without_path.indexOf(c) >= 0) return false;
}
return true;
}
https://docs.oracle.com/javase/jp/6/api/java/util/Formatter.html
@Override
public void onClick(View v) {
if (v == mBuyButton) {
if (mManagedType != Managed.SUBSCRIPTION &&
!service.requestPurchase(mItem, Consts.ITEM_TYPE_INAPP, null)) {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
else if (!service.requestPurchase(mItem, Consts.ITEM_TYPE_SUBSCRIPTION, null)) {
showDialog(DIALOG_SUBSCRIPTIONS_NOT_SUPPORTED_ID);
}
}
}
๋น์์ mManagedType != Managed.SUBSCRIPTION ์กฐ๊ฑด์ด true์ธ ์ํ๋ก ์๊ธฐ ๋๋ฌธ์ else if์ชฝ์ requestPurchase()๊ฐ ํธ์ถ๋๋ฉด ์๋ ์ํฉ์ด์์ผ๋, ์ค์ ๋ก๋ if์ else if์ชฝ์ requestPurchase()๊ฐ ๋ชจ๋ ํธ์ถ๋ ํ์.
์๋ง๋ ์ต์ ํํ๋ ๊ณผ์ ์์ if ~ else if ~ ํ์์ ๋ฌธ์ฅ์ด if ~ else ~ ํ์์ผ๋ก ๊ณ ์ณ์ง๊ณ , requestPurchase()๊ฐ ์กฐ๊ฑด ๋น๊ต ๊ณผ์ ์์ ๋๋ฒ ํธ์ถ์ด ๋๋ ๋ฏ.
if (V != C1 && !F(x, 1)) doThen();
else if (!F(x, 2)) doElse();
// ์ ์ฝ๋๊ฐ...
// ์ด๋ ๊ฒ ํด์๋๋ฉด...
if ( V != C1 && !F(x, 1) && F(x, 2) )
doThen();
else
doElse();
๋ ๋ค๋ฅธ ์์ธ์ผ๋ก, ์ฒซ if๋ฌธ์ !requestPurchase()ํ ๊ฒฐ๊ณผ๊ฐ false๊ฐ ๋ ๊ฒฝ์ฐ, else if์ชฝ์ !requestPurchase()๋ฅผ ํ๊ฐํด์ผ ํ๋ฏ๋ก ๋ ๋ค ์คํ๋ ์ ์๋ค. (๋ฌผ๋ก ๋น์์๋ !requestPurchase() ๊ฒฐ๊ณผ๊ฐ ์ ๋๋ก true๋ก ๋จ์ด์ก๋ค.)
- ๋ฐํ๊ฐ์ด boolean ํ์ด๋๋ผ๋ ์ฌ์ด๋ ์ดํํธ๊ฐ ์๋ ๋ฉ์๋๋ฅผ if๋ฌธ ์กฐ๊ฑด์ ์์ ํธ์ถํ์ง ๋ง๊ฒ.
- else if๋ฅผ ์ฐ์ง ๋ง๊ณ ์ค์ฒฉ๋ if๋ฅผ ์ฌ์ฉ.
if (V == C1) {
if (!F(x, 2)) doThen();
}
else {
if (!F(x, 1)) doElse();
}