• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.googlecode.android_scripting;
18 
19 import android.annotation.NonNull;
20 
21 import com.google.common.base.Strings;
22 
23 public class ConvertUtils {
24     /**
25      * Converts a String of comma separated bytes to a byte array
26      *
27      * @param value The value to convert
28      * @return the byte array
29      */
convertStringToByteArray(String value)30     public static byte[] convertStringToByteArray(String value) {
31         if (value.equals("")) {
32             return new byte[0];
33         }
34         String[] parseString = value.split(",");
35         byte[] byteArray = new byte[parseString.length];
36         if (byteArray.length > 0) {
37             for (int i = 0; i < parseString.length; i++) {
38                 int val = Integer.valueOf(parseString[i].trim());
39                 if (val < 0 || val > 255)
40                     throw new java.lang.NumberFormatException("Val must be between 0 and 255");
41                 byteArray[i] = (byte)val;
42             }
43         }
44         return byteArray;
45     }
46 
47     /**
48      * Converts a byte array to a comma separated String
49      *
50      * @param byteArray
51      * @return comma separated string of bytes
52      */
convertByteArrayToString(byte[] byteArray)53     public static String convertByteArrayToString(byte[] byteArray) {
54         String ret = "";
55         if (byteArray != null) {
56             for (int i = 0; i < byteArray.length; i++) {
57                 if ((i + 1) != byteArray.length) {
58                     ret = ret + Integer.valueOf((byteArray[i]&0xFF)) + ",";
59                 }
60                 else {
61                     ret = ret + Integer.valueOf((byteArray[i]&0xFF));
62                 }
63             }
64         }
65         return ret;
66     }
67 
68     /**
69      * An interface for
70      * @param <T1> The type of the caller.
71      * @param <T2> The returned type of the call.
72      */
73     public interface IFunction<T1, T2> {
call(T1 t1)74         T2 call(T1 t1);
75     }
76 
77     /**
78      * Calls a given function on an object, and returns a NonNull String of the return value.
79      * @param obj The object to get the string data from.
80      * @param function The function or method to call.
81      * @param <T1> The type of the object.
82      * @param <T2> The type of the function return type.
83      * @return A string guaranteed not to be null.
84      */
toNonNullString(T1 obj, @NonNull IFunction<T1, T2> function)85     public static <T1, T2> String toNonNullString(T1 obj, @NonNull IFunction<T1, T2> function) {
86         if (obj == null) {
87             return "";
88         } else {
89             return toNonNullString(function.call(obj));
90         }
91     }
92 
93     /**
94      * Returns toString() or an empty string if {@code obj} or result is null.
95      * @param obj The object to call toString() on.
96      * @param <T> The type of the object.
97      * @return A string guaranteed not to be null.
98      */
toNonNullString(T obj)99     public static <T> String toNonNullString(T obj) {
100         if (obj == null) {
101             return "";
102         } else {
103             return Strings.nullToEmpty(obj.toString());
104         }
105     }
106 }
107