#How to stop this being cursed

1 messages · Page 1 of 1 (latest)

shut mulch
#

I'd like to use these specific sets in the program (password generator). But this does not look healthy. Is there a better way

slender hollowBOT
#

<@&987246399047479336> please have a look, thanks.

slender hollowBOT
flint pivot
near lintel
brave siren
#

Make it all in a normal class, then make singleton static

near lintel
#

i have no idea why you would want to do this

#

you can just "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+"

and if you need a random character use Random or ThreadLocalRandom to get a random number then just string.charAt(randomNumber)

flint pivot
brave siren
#

to support utf8 range and not just ascii.

near lintel
#

passwords usually don't need more than ascii?

#

especially if a program generates them

shut mulch
shut mulch
#

For others: I used a Singleton pattern

package com.cdev;

import java.util.*;

public final class CharacterSetManager {

    private static final CharacterSetManager INSTANCE = new CharacterSetManager();

    private final Map<Sets, Set<Character>> characterSets;

    public enum Sets {
        LOWERCASE,
        UPPERCASE,
        DIGITS,
        SPECIAL
    }

    private CharacterSetManager() {
        Map<Sets, Set<Character>> modifiableMap = new EnumMap<>(Sets.class);
        Set<Character> lowercase = new LinkedHashSet<>();
        Set<Character> uppercase = new LinkedHashSet<>();
        Set<Character> digits = new LinkedHashSet<>();
        Set<Character> specialCharacters = new LinkedHashSet<>(List.of('!', '#', '-', '$', '&'));

        // populate sets
        for (char c = 'a'; c <= 'z'; c++) {
            lowercase.add(c);
            uppercase.add(Character.toUpperCase(c));
        }
        for(char c = '0'; c <= '9'; c++) {
            digits.add(c);
        }

        modifiableMap.put(Sets.LOWERCASE, Collections.unmodifiableSet(lowercase));
        modifiableMap.put(Sets.UPPERCASE, Collections.unmodifiableSet(uppercase));
        modifiableMap.put(Sets.DIGITS, Collections.unmodifiableSet(digits));
        modifiableMap.put(Sets.SPECIAL, Collections.unmodifiableSet(specialCharacters));

        this.characterSets = Collections.unmodifiableMap(modifiableMap);
    }

    public static CharacterSetManager getInstance() {
        return INSTANCE;
    }


    public Set<Character> getSet(Sets set) {
        return this.characterSets.get(set);
    }

    public Map<Sets, Set<Character>> getSetMap() {
        return this.characterSets;
    }
}```
slender hollowBOT
# shut mulch For others: I used a Singleton pattern ``` package com.cdev; import java.util...

Detected code, here are some useful tools:

Formatted code
package com.cdev;

import java.util. * ;

public final class CharacterSetManager {
  private static final CharacterSetManager INSTANCE = new CharacterSetManager();
  private final Map<Sets, Set<Character>> characterSets;
  public enum Sets {
    LOWERCASE, UPPERCASE, DIGITS, SPECIAL}
  private CharacterSetManager() {
    Map<Sets, Set<Character>> modifiableMap = new EnumMap<>(Sets.class );
    Set<Character> lowercase = new LinkedHashSet<>();
    Set<Character> uppercase = new LinkedHashSet<>();
    Set<Character> digits = new LinkedHashSet<>();
    Set<Character> specialCharacters = new LinkedHashSet<>(List.of('!', '#', ' - ', '$', ' & '));
    // populate sets
    for (char c = 'a'; c <= 'z'; c++) {
      lowercase.add(c);
      uppercase.add(Character.toUpperCase(c));
    }
    for (char c = '0'; c <= '9'; c++) {
      digits.add(c);
    }
    modifiableMap.put(Sets.LOWERCASE, Collections.unmodifiableSet(lowercase));
    modifiableMap.put(Sets.UPPERCASE, Collections.unmodifiableSet(uppercase));
    modifiableMap.put(Sets.DIGITS, Collections.unmodifiableSet(digits));
    modifiableMap.put(Sets.SPECIAL, Collections.unmodifiableSet(specialCharacters));
    this .characterSets = Collections.unmodifiableMap(modifiableMap);
  }
  public static CharacterSetManager getInstance() {
    return INSTANCE;
  }
  public Set<Character> getSet(Sets set) {
    return this .characterSets.get(set);
  }
  public Map<Sets, Set<Character>> getSetMap() {
    return this .characterSets;
  }
}
brave siren
#

Maybe make it thread proof?

flint pivot
#

I don't see how this is not thread proof. Hes not changing anything after class initialization and afterwards there are only reads

shut mulch
flint pivot
shut mulch