2017年8月24日木曜日

Kotlin メモ : forEachIndexed

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each-indexed.html


Java
  1. int pos = 1;  
  2. for (Item item : items) {  
  3.     log(pos, item);  
  4.     pos++;  
  5. }  
Kotlin 変換直後
  1. var pos = 1  
  2. for (item in items) {  
  3.     log(pos, item)  
  4.     pos++  
  5. }  
forEachIndexed 使用
  1. items.forEachIndexed { index, item -> log(index + 1, item)}  



2017年8月22日火曜日

Kotlin メモ : mapNotNull

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-not-null.html


  1. val list = (0 until adapter.count)  
  2.         .map { adapter.item(it) }  
  3.         .filterNotNull()  
  4.         .toList()  
mapNotNull 使用
  1. val list = (0 until adapter.count)  
  2.         .mapNotNull { adapter.item(it) }  
  3.         .toList()  

Kotlin メモ : until

https://kotlinlang.org/docs/reference/ranges.html
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/until.html


  1. val list = (0..adapter.count - 1)  
  2.         .map { adapter.item(it) }  
  3.         .filterNotNull()  
  4.         .toList()  
until 使用
  1. val list = (0 until adapter.count)  
  2.         .map { adapter.item(it) }  
  3.         .filterNotNull()  
  4.         .toList()  

Kotlin メモ : filterNotNull

filterNotNull


Java
  1. final List<Item> list = new ArrayList<>();  
  2. for (int i = 0, count = adapter.getCount(); i < count; i++) {  
  3.     final Item item = adapter.getItem(i);  
  4.     if (item != null) {  
  5.         list.add(item);  
  6.     }  
  7. }  
Kotlin 自動変換直後
  1. val list = ArrayList<Item>()  
  2. var i = 0  
  3. val count = adapter.count  
  4. while (i < count) {  
  5.     val item = adapter.item(i)  
  6.     if (item != null) {  
  7.         list.add(item)  
  8.     }  
  9.     i++  
  10. }  
range, let 使用
  1. val list = ArrayList<Item>()  
  2. for(i in 0..adapter.count - 1) {  
  3.     adapter.item(i)?.let {   
  4.         list.add(it)  
  5.     }  
  6. }  
map, filterNotNull 使用
  1. val list = (0..adapter.count - 1)  
  2.         .map { adapter.item(it) }  
  3.         .filterNotNull()  
  4.         .toList()  

2017年8月16日水曜日

Kotlin メモ : indexOfFirst

indexOfFirst

predicate にマッチする最初の位置の index を返す。マッチするものが無い場合は -1 を返す。


Java
  1. @Override  
  2. public int getSectionForPosition(int position) {  
  3.     final Object[] sections = getSections();  
  4.     if (sections == null) {  
  5.         return 0;  
  6.     }  
  7.     int section = 0;  
  8.     for (int i = 0; i < sections.length; i++) {  
  9.         final MonthSection ms = (MonthSection) sections[i];  
  10.         if (ms.position > position) {  
  11.             return section;  
  12.         }  
  13.         section = i;  
  14.     }  
  15.     return section;  
  16. }  
Kotlin
  1. override fun getSectionForPosition(position: Int): Int {  
  2.     val sections = getSections() ?: return 0  
  3.     return sections.indexOfFirst { it.position > position }  
  4.             .let {  
  5.                 when {  
  6.                     it > 0 -> it - 1  
  7.                     it == 0 -> 0  
  8.                     else -> sections.lastIndex  
  9.                 }  
  10.             }  
  11. }  

2017年8月10日木曜日

Kotlin メモ : ArrayAdapter

Java
  1. class MyAdapter extends ArrayAdapter<MyData> {  
  2.   
  3.     private final LayoutInflater inflater;  
  4.   
  5.     MyAdapter(Context context, List<MyData> objects) {  
  6.         super(context, 0, objects);  
  7.         inflater = LayoutInflater.from(context);  
  8.     }  
  9.   
  10.     ...  
  11.   
  12.     @NonNull  
  13.     @Override  
  14.     public View getView(int position, View convertView, @NonNull ViewGroup parent) {  
  15.         final MyViewHolder holder;  
  16.         if (convertView == null) {  
  17.             holder = MyViewHolder.create(inflater, parent);  
  18.             convertView = holder.view;  
  19.             convertView.setTag(holder);  
  20.         } else {  
  21.             holder = (MyViewHolder) convertView.getTag();  
  22.         }  
  23.   
  24.         final MyData data = getItem(position);  
  25.         assert data != null;  
  26.   
  27.         holder.bind(data);  
  28.   
  29.         return convertView;  
  30.     }  
  31. }  
Kotlin
  1. class MyAdapter(context: Context, objects: List<MyData>) : ArrayAdapter<MyData>(context, 0, objects) {  
  2.   
  3.     private val inflater = LayoutInflater.from(context)  
  4.   
  5.     ...  
  6.   
  7.     override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {  
  8.         val view: View = convertView ?: MyViewHolder.create(inflater, parent)  
  9.                 .also { it.view.tag = it }  
  10.                 .view  
  11.         getItem(position)?.let {  
  12.             (view.tag as MyViewHolder).bind(it)  
  13.         }  
  14.         return view  
  15.     }  
  16. }  

2017年8月3日木曜日

Kotlin メモ : joinToString

kotlin-stdlib / kotlin.collections / joinToString

Java
  1. /** 
  2.  * 00 11 22 33 44 55 66 77 
  3.  */  
  4. @NonNull  
  5. public String expression(byte[] bytes) {  
  6.     final StringBuilder sb = new StringBuilder();  
  7.     boolean firstTime = true;  
  8.     for (byte each : bytes) {  
  9.         if (firstTime) {  
  10.             firstTime = false;  
  11.         } else {  
  12.             sb.append(" ");  
  13.         }  
  14.         sb.append(hex(each));  
  15.     }  
  16.     return sb.toString();  
  17. }  
Java その2
  1. /** 
  2.  * 00 11 22 33 44 55 66 77 
  3.  */  
  4. @NonNull  
  5. public String expression(byte[] bytes) {  
  6.     final List<String> tokens = new ArrayList<>();  
  7.     for (byte each : bytes) {  
  8.         tokens.add(hex(each));  
  9.     }  
  10.     return TextUtils.join(" ", tokens);  
  11. }  
Kotlin
  1. /** 
  2.  * 00 11 22 33 44 55 66 77 
  3.  */  
  4. fun expression(bytes : ByteArray): String {  
  5.     return bytes.joinToString(separator = " ", transform = { hex(it) })  
  6. }  

2017年8月1日火曜日

Kotlin メモ : takeIf

Java
  1. public Hoge createFromParcel(Parcel source) {  
  2.     final int length = source.readInt();  
  3.  final byte[] data;  
  4.     if (length > 0) {  
  5.         data = new byte[length];  
  6.         source.readByteArray(data);  
  7.     } else {  
  8.         data = null;  
  9.     }  
  10.   
  11.     return new Hoge(data);  
  12. }  
Kotlin 変換直後
  1. override fun createFromParcel(source: Parcel): Hoge {  
  2.     val length = source.readInt()  
  3.     val data: ByteArray?  
  4.     if (length > 0) {  
  5.         data = ByteArray(length)  
  6.         source.readByteArray(data)  
  7.     } else {  
  8.         data = null  
  9.     }  
  10.   
  11.     return Hoge(data)  
  12. }  
source.run {} を使う
  1. override fun createFromParcel(source: Parcel): Hoge = source.run {  
  2.     val length = readInt()  
  3.     val data: ByteArray?  
  4.     if (length > 0) {  
  5.         data = ByteArray(length)  
  6.         readByteArray(data)  
  7.     } else {  
  8.         data = null  
  9.     }  
  10.   
  11.     Hoge(data)  
  12. }  
also を使う
  1. override fun createFromParcel(source: Parcel): Hoge = source.run {  
  2.     val length = readInt()  
  3.     val data: ByteArray?  
  4.     if (length > 0) {  
  5.         data = ByteArray(length).also { readByteArray(it) }  
  6.     } else {  
  7.         data = null  
  8.     }  
  9.   
  10.     Hoge(data)  
  11. }  
if 式にしてみる
  1. override fun createFromParcel(source: Parcel): Hoge = source.run {  
  2.     val length = readInt()  
  3.     val data: ByteArray? = if (length > 0) {  
  4.         ByteArray(length).also { readByteArray(it) }  
  5.     } else {  
  6.         null  
  7.     }  
  8.   
  9.     Hoge(data)  
  10. }  
length に let を使う
  1. override fun createFromParcel(source: Parcel): Hoge = source.run {  
  2.     val length = readInt()  
  3.     val data: ByteArray? = if (length > 0) {  
  4.         length.let { ByteArray(it).also { readByteArray(it) } }  
  5.     } else {  
  6.         null  
  7.     }  
  8.   
  9.     Hoge(data)  
  10. }  
readInt() に takeIf を使う
  1. override fun createFromParcel(source: Parcel): Hoge = source.run {  
  2.     val data: ByteArray? = readInt()  
  3.             .takeIf { it > 0 }  
  4.             ?.let { ByteArray(it).also { readByteArray(it) } }  
  5.   
  6.     Hoge(data)  
  7. }