All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Util.HashtableUtil Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Basit Anwer
 */
/// 
/// Provide methods to convert hashtable into a String form, and repopulating
/// hastable from String. The conversion do not save type information and assumes
/// that keys are of int type, while values are of String type
/// 
public class HashtableUtil {
    /// 
    /// Convert hastable to a String form
    /// 
    /// Hashtable containg int key and String value
    /// String representation of hashtable
    public static String ToString(HashMap table) {

        if (table != null) {
            StringBuilder toStr = new StringBuilder();
            Iterator hasTable = table.entrySet().iterator();
            Map.Entry pair;
            while (hasTable.hasNext()) {
                pair = (Map.Entry) hasTable.next();
                toStr.append(pair.getKey().toString() + "$" + pair.getValue().toString() + "\r\n");
                //toStr.append("{0}${1}\r\n", pair.getKey(), pair.getValue());
            }
            return toStr.toString();
        }
        return "";
    }

    public static String ToString(ArrayList list) {

        if (list != null) {
            StringBuilder toStr = new StringBuilder();
            for (Object entry : list) {
                toStr.append(entry.toString() + "\r\n");
            }
            return toStr.toString();
        }
        return "";
    }

    /// 
    /// Populate a hastable from its String representation
    /// 
    /// String representation of hashtable
    /// Hashtable formed from String representation
    public static HashMap FromString(String rep) {
        if (rep != null && rep.length() > 0) {
            HashMap table = new HashMap();
            String[] entries = rep.split("\r\n");
            //String[] entries = rep.split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            for (String entry : entries) {
                String[] keyVal = entry.split("\\$");
                table.put(Integer.decode(keyVal[0]), keyVal[1]);
            }
            return table;
        }
        return null;
    }


    public static Map extractKeyStatus(HashMap keysFound, String[] keys) {
        java.util.ArrayList availableKeys = null;
        java.util.Map keyStatus = new java.util.HashMap();

        if (keysFound.containsKey("items-found"))
        {
            availableKeys = (java.util.ArrayList)keysFound.get("items-found");
        }

//C# TO JAVA CONVERTER TODO TASK: There is no equivalent to implicit typing in Java:
        for (String key : keys)
        {
            keyStatus.put(key, false);

            if (availableKeys != null && availableKeys.contains(key))
            {
                keyStatus.put(key, true);
            }
        }

        return keyStatus;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy