Util Parse multiple UUIDS to string

Discussion in 'Resources' started by w0lv3s, Nov 16, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    w0lv3s

    Not sure where to post this, and not much happening here, but hopefully this basic junk is useful to somebody as I find it works quite well for me when storing and retrieving values from a database... It's just a couple methods that pile a bunch of UUIDs together in one string from an ArrayList, and alternatively create a bunch of UUID objects in an ArrayList from a string... Hopefully it's useful to somebody, it works great for me, fairly basic...

    Code:
        private static final char UUID_SEPARATOR = ',';
    
        protected String uuidListToString(ArrayList<UUID> uuids) {
            StringBuilder uuidList = new StringBuilder();
    
            boolean firstIteration = true;
            for (UUID currentUuid : uuids) {
                if (firstIteration) {
                    uuidList.append(currentUuid.toString());
                    firstIteration = false;
                }
                else {
                    uuidList.append(UUID_SEPARATOR).append(currentUuid.toString());
                }
            }
    
            return uuidList.toString();
        }
    
        public static ArrayList<UUID> uuidListFromString(String string) {
            ArrayList<UUID> uuids = new ArrayList<>();
    
            if (string != null) {
                char[] stringArray = string.toCharArray();
                String currentUuidString = "";
    
                for (int i = 0; i < string.length(); i++) {
                    if (stringArray[i] != UUID_SEPARATOR) {
                        currentUuidString += stringArray[i];
                    } else {
                        try {
                            uuids.add(UUID.fromString(currentUuidString));
                        } catch (IllegalArgumentException e) {
                            YOUR_PLUGIN.getPlugin().getLogger().warning("Could not parse UUID from string");
                        }
                        currentUuidString = "";
                    }
                }
            }
            return uuids;
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to resources
    @w0lv3s And why would you put them together in the same string?
    Why not just save them as a list?
     
    Maxx_Qc likes this.
Thread Status:
Not open for further replies.

Share This Page