This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void criaInterface() { | |
String var = "content"; | |
botao.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
System.out.println(var); // ERRO! | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void criaInterface() { | |
final String var = "content"; | |
botao.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
System.out.println(var); // OK! | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Before | |
public void setup() { | |
names = new ArrayList<Option<String>>(); | |
names.add(new Some<String>("Renan")); | |
names.add(new None<String>()); | |
names.add(new Some<String>("Paulo")); | |
} | |
@Test | |
public void usandoGetOrElse() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void verificaSePossuiValor() { | |
String[] expected = { "Renan", null, "Paulo"}; | |
for (int i = 0; i < names.size(); i++) { | |
Option<String> option = names.get(i); | |
if (option.hasValue()) { | |
String value = option.get(); | |
System.out.println(option + " = " + value); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Option<String> fazAlgo() { | |
if(se tenho valor retornarei um Some com o valor) { | |
return new Some<String>(valor); | |
} else { | |
// não tenho valor, ao invés de retornar nulo | |
//e correr o risco do código cliente chamar um método em null, retornarei um None | |
return new None<String>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Option<T> { | |
public abstract boolean hasValue(); | |
public abstract T get(); | |
public T getOrElse(T alternative) { | |
return hasValue() == true ? get() : alternative; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Some<T> extends Option<T> { | |
private final T value; | |
public Some(T value) { this.value = value; } | |
public boolean hasValue() { return true; } | |
public T get() { return value; } | |
@Override |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class None<T> extends Option<T> { | |
public static class NoneHasNoValue extends RuntimeException {} | |
public None() {} | |
public boolean hasValue() { return false; } | |
public T get() { throw new NoneHasNoValue(); } | |
@Override |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
... | |
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); | |
String regid = getRegistrationId(context); | |
if (regid.isEmpty()) { | |
registerInBackground(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void registerInBackground() { | |
new AsyncTask() { | |
@Override | |
protected String doInBackground(Object[] params) { | |
regid = gcm.register(SENDER_ID); | |
sendRegistrationIdToBackend(regid); | |
storeRegistrationId(context, regid); | |
... | |
} |
OlderNewer