Skip to content

Instantly share code, notes, and snippets.

@wzieba
Forked from pasmat/Boxer.java
Last active October 7, 2018 07:56
Show Gist options
  • Save wzieba/578827ce40794f887ded090a382fd537 to your computer and use it in GitHub Desktop.
Save wzieba/578827ce40794f887ded090a382fd537 to your computer and use it in GitHub Desktop.
Data-binded values that are boxed(such as ones that come from Kotlin, as there's no primitive types in Kotlin..) requires safe unboxing, so there's no NPE(null pointer exception) when binding data. Theres a method for this safeUnbox inside Android SDK, however this doesn't support two-way data binding. Therefore we have to define these two-way s…
package fi.matalamaki.util;
import androidx.databinding.InverseMethod;
/**
* Data-binded values that are boxed(such as ones that come from Kotlin,
* as there's no primitive types in Kotlin..) requires safe unboxing,
* so there's no NPE(null pointer exception) when binding data.
*
* Theres a method for this safeUnbox inside Android SDK,
* however this doesn't support two-way data binding.
*
* Therefore we have to define these two-way safe unboxing methods by ourselves.
*
* example usage inside xml
*
*
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
<data>
<import type="fi.matalamaki.util.Boxer"/>
<variable
name="intParameter"
type="java.lang.Integer"/>
</data>
<SeekBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:progress="@={Boxer.unbox(intParameter)}"/>
</layout>
*
*
*/
public class Boxer {
private static final char DEFAULT_CHAR = ' ';
private static final int DEFAULT_NUMBER = 0;
@InverseMethod("boxBoolean")
public static boolean unbox(Boolean b) {
return (b != null) && b;
}
public static Boolean boxBoolean(boolean b) {
return b;
}
@InverseMethod("boxChar")
public static char unbox(Character c) {
return c != null ? c : DEFAULT_CHAR;
}
public static Character boxChar(char c) {
return c;
}
@InverseMethod("boxByte")
public static byte unbox(Byte b) {
return b != null ? b : DEFAULT_NUMBER;
}
public static Byte boxByte(byte b) {
return b;
}
@InverseMethod("boxShort")
public static short unbox(Short s) {
return s != null ? s : DEFAULT_NUMBER;
}
public static Short boxShort(short s) {
return s;
}
@InverseMethod("boxInteger")
public static int unbox(Integer i) {
return i != null ? i : DEFAULT_NUMBER;
}
public static Integer boxInteger(int i) {
return i;
}
@InverseMethod("boxInteger")
public static long unbox(Long l) {
return l != null ? l : DEFAULT_NUMBER;
}
public static Long boxLong(long l) {
return l;
}
@InverseMethod("boxFloat")
public static float unbox(Float f) {
return f != null ? f : DEFAULT_NUMBER;
}
public static Float boxFloat(float f) {
return f;
}
@InverseMethod("boxDouble")
public static double unbox(Double d) {
return (d != null) ? d : DEFAULT_NUMBER;
}
public static Double boxDouble(double d) {
return d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment