Example usage for com.google.common.primitives UnsignedInteger valueOf

List of usage examples for com.google.common.primitives UnsignedInteger valueOf

Introduction

In this page you can find the example usage for com.google.common.primitives UnsignedInteger valueOf.

Prototype

public static UnsignedInteger valueOf(String string) 

Source Link

Document

Returns an UnsignedInteger holding the value of the specified String , parsed as an unsigned int value.

Usage

From source file:com.lyndir.masterpassword.CLI.java

public static void main(final String[] args) throws IOException {

    // Read information from the environment.
    char[] masterPassword;
    String siteName = null, context = null;
    String userName = System.getenv(MPConstant.env_userName);
    String siteTypeName = ifNotNullElse(System.getenv(MPConstant.env_siteType), "");
    MPSiteType siteType = siteTypeName.isEmpty() ? MPSiteType.GeneratedLong
            : MPSiteType.forOption(siteTypeName);
    MPSiteVariant variant = MPSiteVariant.Password;
    String siteCounterName = ifNotNullElse(System.getenv(MPConstant.env_siteCounter), "");
    UnsignedInteger siteCounter = siteCounterName.isEmpty() ? UnsignedInteger.valueOf(1)
            : UnsignedInteger.valueOf(siteCounterName);

    // Parse information from option arguments.
    boolean userNameArg = false, typeArg = false, counterArg = false, variantArg = false, contextArg = false;
    for (final String arg : Arrays.asList(args))
        // Full Name
        if ("-u".equals(arg) || "--username".equals(arg))
            userNameArg = true;/* w  w  w  . ja va 2  s .c  o m*/
        else if (userNameArg) {
            userName = arg;
            userNameArg = false;
        }

        // Type
        else if ("-t".equals(arg) || "--type".equals(arg))
            typeArg = true;
        else if (typeArg) {
            siteType = MPSiteType.forOption(arg);
            typeArg = false;
        }

        // Counter
        else if ("-c".equals(arg) || "--counter".equals(arg))
            counterArg = true;
        else if (counterArg) {
            siteCounter = UnsignedInteger.valueOf(arg);
            counterArg = false;
        }

        // Variant
        else if ("-v".equals(arg) || "--variant".equals(arg))
            variantArg = true;
        else if (variantArg) {
            variant = MPSiteVariant.forOption(arg);
            variantArg = false;
        }

        // Context
        else if ("-C".equals(arg) || "--context".equals(arg))
            contextArg = true;
        else if (contextArg) {
            context = arg;
            contextArg = false;
        }

        // Help
        else if ("-h".equals(arg) || "--help".equals(arg)) {
            System.out.println();
            System.out.format("Usage: mpw [-u name] [-t type] [-c counter] site\n\n");
            System.out.format("    -u name      Specify the full name of the user.\n");
            System.out.format("                 Defaults to %s in env.\n\n", MPConstant.env_userName);
            System.out.format("    -t type      Specify the password's template.\n");
            System.out.format(
                    "                 Defaults to %s in env or 'long' for password, 'name' for login.\n",
                    MPConstant.env_siteType);

            int optionsLength = 0;
            Map<String, MPSiteType> typeMap = Maps.newLinkedHashMap();
            for (MPSiteType elementType : MPSiteType.values()) {
                String options = Joiner.on(", ").join(elementType.getOptions());
                typeMap.put(options, elementType);
                optionsLength = Math.max(optionsLength, options.length());
            }
            for (Map.Entry<String, MPSiteType> entry : typeMap.entrySet()) {
                String infoString = strf("                  -v %" + optionsLength + "s | ", entry.getKey());
                String infoNewline = "\n" + StringUtils.repeat(" ", infoString.length() - 3) + " | ";
                infoString += entry.getValue().getDescription().replaceAll("\n", infoNewline);
                System.out.println(infoString);
            }
            System.out.println();

            System.out.format("    -c counter   The value of the counter.\n");
            System.out.format("                 Defaults to %s in env or '1'.\n\n", MPConstant.env_siteCounter);
            System.out.format("    -v variant   The kind of content to generate.\n");
            System.out.format("                 Defaults to 'password'.\n");

            optionsLength = 0;
            Map<String, MPSiteVariant> variantMap = Maps.newLinkedHashMap();
            for (MPSiteVariant elementVariant : MPSiteVariant.values()) {
                String options = Joiner.on(", ").join(elementVariant.getOptions());
                variantMap.put(options, elementVariant);
                optionsLength = Math.max(optionsLength, options.length());
            }
            for (Map.Entry<String, MPSiteVariant> entry : variantMap.entrySet()) {
                String infoString = strf("                  -v %" + optionsLength + "s | ", entry.getKey());
                String infoNewline = "\n" + StringUtils.repeat(" ", infoString.length() - 3) + " | ";
                infoString += entry.getValue().getDescription().replaceAll("\n", infoNewline);
                System.out.println(infoString);
            }
            System.out.println();

            System.out.format("    -C context   A variant-specific context.\n");
            System.out.format("                 Defaults to empty.\n");
            for (Map.Entry<String, MPSiteVariant> entry : variantMap.entrySet()) {
                String infoString = strf("                  -v %" + optionsLength + "s | ", entry.getKey());
                String infoNewline = "\n" + StringUtils.repeat(" ", infoString.length() - 3) + " | ";
                infoString += entry.getValue().getContextDescription().replaceAll("\n", infoNewline);
                System.out.println(infoString);
            }
            System.out.println();

            System.out.format("    ENVIRONMENT\n\n");
            System.out.format("        MP_USERNAME    | The full name of the user.\n");
            System.out.format("        MP_SITETYPE    | The default password template.\n");
            System.out.format("        MP_SITECOUNTER | The default counter value.\n\n");
            return;
        } else
            siteName = arg;

    // Read missing information from the console.
    Console console = System.console();
    try (InputStreamReader inReader = new InputStreamReader(System.in)) {
        LineReader lineReader = new LineReader(inReader);

        if (siteName == null) {
            System.err.format("Site name: ");
            siteName = lineReader.readLine();
        }

        if (userName == null) {
            System.err.format("User's name: ");
            userName = lineReader.readLine();
        }

        if (console != null)
            masterPassword = console.readPassword("%s's master password: ", userName);

        else {
            System.err.format("%s's master password: ", userName);
            masterPassword = lineReader.readLine().toCharArray();
        }
    }

    // Encode and write out the site password.
    System.out.println(MasterKey.create(userName, masterPassword).encode(siteName, siteType, siteCounter,
            variant, context));
}

From source file:c1c.v8fs.IndexEntry.java

@Override
public void writeToBuffer(ByteBuffer buffer) {

    buffer.putInt(Integer.reverseBytes(UnsignedInteger.valueOf(attributesAddress).intValue()))
            .putInt(Integer.reverseBytes(UnsignedInteger.valueOf(contentAddress).intValue()))
            .putInt(Integer.reverseBytes(UnsignedInteger.valueOf(reserved).intValue()));
}

From source file:c1c.v8fs.ContainerHeader.java

@Override
public void writeToBuffer(ByteBuffer buffer) {
    buffer.putInt(Integer.reverseBytes(Integer.MAX_VALUE))
            .putInt(Integer.reverseBytes(UnsignedInteger.valueOf(defaultChunkSize).intValue()))
            .putInt(Integer.reverseBytes(UnsignedInteger.valueOf(reservedMaybeFilesCount).intValue()))
            .putInt(Integer.reverseBytes(0));
}

From source file:com.spotify.crtauth.utils.RealTimeSupplier.java

@Override
public UnsignedInteger getTime() {
    long currentTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    return UnsignedInteger.valueOf(currentTime);
}

From source file:com.spotify.crtauth.utils.SettableTimeSupplier.java

@Override
public UnsignedInteger getTime() {
    return UnsignedInteger.valueOf(time);
}

From source file:org.immutables.fixture.SillyEntity.java

@Value.Derived
public UnsignedInteger der() {
    return UnsignedInteger.valueOf(1);
}

From source file:com.wrmsr.wava.core.literal.I32Literal.java

public UnsignedInteger u() {
    return UnsignedInteger.valueOf(bits);
}

From source file:org.apache.nifi.processors.evtx.parser.NumberUtil.java

/**
 * Throws an exception if the UnsignedInteger is greater than a given int, returning the int value otherwise
 *
 * @param unsignedInteger the number//from w w w .j ava 2s  .c om
 * @param max             the maximum value
 * @param errorMessage    error message (can be Java format string)
 * @param args            args for error message format string
 * @return the value
 * @throws IOException if the value is greater than max
 */
public static int intValueMax(UnsignedInteger unsignedInteger, int max, String errorMessage, Object... args)
        throws IOException {
    if (unsignedInteger.compareTo(UnsignedInteger.valueOf(max)) > 0) {
        throw createException(errorMessage, args, "< " + max, unsignedInteger);
    }
    return unsignedInteger.intValue();
}

From source file:pw.simplyintricate.bitcoin.models.datastructures.VariableInteger.java

public static VariableInteger fromInputStream(LittleEndianDataInputStream reader) throws IOException {
    byte determiningSize = reader.readByte();
    UnsignedInteger value;//  ww  w .ja v a 2 s. c o  m
    if (determiningSize < 0xfd) {
        value = UnsignedInteger.fromIntBits(determiningSize);
    } else if (determiningSize <= 0xffff) {
        int followUpValue = reader.readUnsignedShort();
        value = UnsignedInteger.fromIntBits(followUpValue);
    } else if (determiningSize <= 0xffffffff) {
        int followUpValue = reader.readInt();
        value = UnsignedInteger.valueOf(followUpValue);
    } else {
        long followUpValue = reader.readLong();
        value = UnsignedInteger.valueOf(followUpValue);
    }

    VariableInteger variableInteger = new VariableInteger(value);

    return variableInteger;
}

From source file:com.torodb.mongodb.language.ObjectIdFactory.java

public BsonObjectId consumeObjectId() {
    long secs = System.currentTimeMillis() / 1000;
    return new IntBasedBsonObjectId(UnsignedInteger.valueOf(secs).intValue(), MACHINE_ID, PROCESS_ID,
            COUNTER.getAndIncrement() & 0xFFFFFF);
}