Example usage for java.util HashSet toArray

List of usage examples for java.util HashSet toArray

Introduction

In this page you can find the example usage for java.util HashSet toArray.

Prototype

Object[] toArray();

Source Link

Document

Returns an array containing all of the elements in this set.

Usage

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void andNotBigIntsTest() {
    RoaringBitmap rb = new RoaringBitmap();
    RoaringBitmap rb2 = new RoaringBitmap();
    HashSet<Integer> hs = new HashSet<Integer>();

    for (int i = 1 << 31; i < (1 << 31) + 65536; i += 2) {
        rb.add(i);/* ww  w . j  a va 2s .  c o  m*/
        hs.add(i);
    }
    for (int i = (1 << 31) + 3 * 65536; i < (1 << 31) + 4 * 65536; i += 3) {
        rb.add(i);
    }
    for (int i = (1 << 31) + 5 * 65536; i < (1 << 31) + 6 * 65536; i += 3) {
        hs.add(i);
        rb.add(i);
    }
    for (int i = (1 << 31) + 9 * 65536; i < (1 << 31) + 10 * 65536; i += 5) {
        rb.add(i);
    }

    for (int i = (1 << 31) + 3 * 65536; i < (1 << 31) + 4 * 65536; i += 3) {
        rb2.add(i);
    }
    for (int i = (1 << 31) + 6 * 65536; i < (1 << 31) + 7 * 65536; i += 4) {
        rb2.add(i);
    }
    for (int i = (1 << 31) + 9 * 65536; i < (1 << 31) + 10 * 65536; i += 5) {
        rb2.add(i);
    }
    for (int i = (1 << 31) + 13 * 65536; i < (1 << 31) + 14 * 65536; i += 7) {
        rb2.add(i);
    }

    RoaringBitmap rbandnot = RoaringBitmap.andNot(rb, rb2);

    Object[] correct = hs.toArray();
    Arrays.sort(correct);
    Integer[] resandNot = ArrayUtils.toObject(rbandnot.toArray());
    Assert.assertArrayEquals(correct, resandNot);
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void orBigIntsTest() {
    RoaringBitmap rb = new RoaringBitmap();
    RoaringBitmap rb2 = new RoaringBitmap();
    HashSet<Integer> hs = new HashSet<Integer>();

    for (int i = 1 << 31; i < (1 << 31) + 65536; i += 2) {
        rb.add(i);//w  w  w  .  ja v  a 2  s  . c o m
        hs.add(i);
    }
    for (int i = (1 << 31) + 3 * 65536; i < (1 << 31) + 4 * 65536; i += 3) {
        hs.add(i);
        rb.add(i);
    }
    for (int i = (1 << 31) + 5 * 65536; i < (1 << 31) + 7 * 65536; i += 5) {
        hs.add(i);
        rb.add(i);
    }
    for (int i = (1 << 31) + 9 * 65536; i < (1 << 31) + 10 * 65536; i += 3) {
        hs.add(i);
        rb.add(i);
    }

    for (int i = 1 << 31 + 3 * 65536; i < (1 << 31) + 4 * 65536; i += 5) {
        hs.add(i);
        rb2.add(i);
    }
    for (int i = (1 << 31) + 6 * 65536; i < (1 << 31) + 7 * 65536; i += 3) {
        hs.add(i);
        rb2.add(i);
    }
    for (int i = (1 << 31) + 9 * 65536; i < (1 << 31) + 11 * 65536; i += 5) {
        hs.add(i);
        rb2.add(i);
    }
    for (int i = (1 << 31) + 13 * 65536; i < (1 << 31) + 14 * 65536; i += 7) {
        hs.add(i);
        rb2.add(i);
    }

    RoaringBitmap rbor = RoaringBitmap.or(rb, rb2);

    Object[] correct = hs.toArray();
    Arrays.sort(correct);
    Integer[] resor = ArrayUtils.toObject(rbor.toArray());
    Assert.assertArrayEquals(correct, resor);
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void andBigIntsTest() {
    RoaringBitmap rb = new RoaringBitmap();
    RoaringBitmap rb2 = new RoaringBitmap();
    HashSet<Integer> hs = new HashSet<Integer>();

    for (int i = 1 << 31; i < (1 << 31) + 65536; i += 2) {
        rb.add(i);//from w ww  . ja  v a 2 s.c o  m
    }
    for (int i = (1 << 31) + 3 * 65536; i < (1 << 31) + 4 * 65536; i += 3) {
        rb.add(i);
    }
    for (int i = (1 << 31) + 5 * 65536; i < (1 << 31) + 7 * 65536; i += 3) {
        rb.add(i);
    }
    for (int i = (1 << 31) + 6 * 65536; i < (1 << 31) + 7 * 65536; i += 4) {
        rb.add(i);
    }
    for (int i = (1 << 31) + 9 * 65536; i < (1 << 31) + 10 * 65536; i += 5) {
        rb.add(i);
    }

    for (int i = (1 << 31) + 3 * 65536; i < (1 << 31) + 4 * 65536; i += 3) {
        hs.add(i);
        rb2.add(i);
    }
    for (int i = (1 << 31) + 6 * 65536; i < (1 << 31) + 7 * 65536; i += 4) {
        hs.add(i);
        rb2.add(i);
    }
    for (int i = (1 << 31) + 9 * 65536; i < (1 << 31) + 10 * 65536; i += 5) {
        hs.add(i);
        rb2.add(i);
    }
    for (int i = (1 << 31) + 13 * 65536; i < (1 << 31) + 14 * 65536; i += 7) {
        rb2.add(i);
    }

    RoaringBitmap rband = RoaringBitmap.and(rb, rb2);

    Object[] correct = hs.toArray();
    Arrays.sort(correct);
    Integer[] resand = ArrayUtils.toObject(rband.toArray());
    Assert.assertArrayEquals(correct, resand);
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void xorBigIntsTest() {
    RoaringBitmap rb = new RoaringBitmap();
    RoaringBitmap rb2 = new RoaringBitmap();
    HashSet<Integer> hs = new HashSet<Integer>();

    for (int i = 1 << 31; i < (1 << 31) + 65536; i += 2) {
        hs.add(i);/*from   www. j  ava 2  s.c o  m*/
        rb.add(i);
    }
    for (int i = (1 << 31) + 3 * 65536; i < (1 << 31) + 4 * 65536; i += 3) {
        rb.add(i);
    }
    for (int i = (1 << 31) + 5 * 65536; i < (1 << 31) + 7 * 65536; i += 5) {
        rb.add(i);
    }
    for (int i = (1 << 31) + 9 * 65536; i < (1 << 31) + 10 * 65536; i += 7) {
        rb.add(i);
    }
    for (int i = (1 << 31) + 11 * 65536; i < (1 << 31) + 12 * 65536; i += 6) {
        hs.add(i);
        rb.add(i);
    }

    for (int i = (1 << 31) + 3 * 65536; i < (1 << 31) + 4 * 65536; i += 3) {
        rb2.add(i);
    }
    for (int i = (1 << 31) + 5 * 65536; i < (1 << 31) + 7 * 65536; i += 5) {
        rb2.add(i);
    }
    for (int i = (1 << 31) + 9 * 65536; i < (1 << 31) + 10 * 65536; i += 7) {
        rb2.add(i);
    }
    for (int i = (1 << 31) + 13 * 65536; i < (1 << 31) + 14 * 65536; i += 2) {
        hs.add(i);
        rb2.add(i);
    }

    RoaringBitmap rbxor = RoaringBitmap.xor(rb, rb2);

    Object[] correct = hs.toArray();
    Arrays.sort(correct);
    Integer[] resxor = ArrayUtils.toObject(rbxor.toArray());
    Assert.assertArrayEquals(correct, resxor);
}

From source file:com.microsoft.azure.storage.table.TableEncryptionPolicy.java

/**
 * Return an encrypted entity. This method is used for encrypting entity properties.
 *///from w  w w .j  ava  2s.  c o m
Map<String, EntityProperty> encryptEntity(Map<String, EntityProperty> properties, String partitionKey,
        String rowKey, EncryptionResolver encryptionResolver) throws StorageException {
    Utility.assertNotNull("properties", properties);

    // The Key should be set on the policy for encryption. Otherwise, throw an error.
    if (this.keyWrapper == null) {
        throw new IllegalArgumentException(SR.KEY_MISSING);
    }

    EncryptionData encryptionData = new EncryptionData();
    encryptionData.setEncryptionAgent(new EncryptionAgent(Constants.EncryptionConstants.ENCRYPTION_PROTOCOL_V1,
            EncryptionAlgorithm.AES_CBC_256));

    try {
        Map<String, EntityProperty> encryptedProperties = new HashMap<String, EntityProperty>();
        HashSet<String> encryptionPropertyDetailsSet = new HashSet<String>();

        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(256);

        Cipher myAes = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKey aesKey = keyGen.generateKey();
        myAes.init(Cipher.ENCRYPT_MODE, aesKey);

        // Wrap key
        Pair<byte[], String> encryptedKey = this.keyWrapper
                .wrapKeyAsync(aesKey.getEncoded(), null /* algorithm */).get();
        encryptionData.setWrappedContentKey(new WrappedContentKey(this.keyWrapper.getKid(),
                encryptedKey.getKey(), encryptedKey.getValue()));

        encryptionData.setContentEncryptionIV(myAes.getIV());

        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        for (Map.Entry<String, EntityProperty> kvp : properties.entrySet()) {
            if (encryptionResolver != null
                    && encryptionResolver.encryptionResolver(partitionKey, rowKey, kvp.getKey())) {
                // Throw if users try to encrypt null properties. This could happen in the DynamicTableEntity case
                // where a user adds a new property as follows - ent.Properties.Add("foo2", null);
                if (kvp.getValue() == null) {
                    throw new IllegalArgumentException(SR.ENCRYPTING_NULL_PROPERTIES_NOT_ALLOWED);
                }

                kvp.getValue().setIsEncrypted(true);
            }

            // IsEncrypted is set to true when either the EncryptPropertyAttribute is set on a property or when it is 
            // specified in the encryption resolver or both.
            if (kvp.getValue() != null && kvp.getValue().isEncrypted()) {
                // Throw if users try to encrypt non-string properties.
                if (kvp.getValue().getEdmType() != EdmType.STRING) {
                    throw new IllegalArgumentException(String
                            .format(SR.UNSUPPORTED_PROPERTY_TYPE_FOR_ENCRYPTION, kvp.getValue().getEdmType()));
                }

                byte[] columnIVFull = digest
                        .digest(Utility.binaryAppend(encryptionData.getContentEncryptionIV(),
                                (partitionKey + rowKey + kvp.getKey()).getBytes(Constants.UTF8_CHARSET)));

                byte[] columnIV = new byte[16];
                System.arraycopy(columnIVFull, 0, columnIV, 0, 16);
                myAes.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(columnIV));

                // Throw if users try to encrypt null properties. This could happen in the DynamicTableEntity or POCO
                // case when the property value is null.
                if (kvp.getValue() == null) {
                    throw new IllegalArgumentException(SR.ENCRYPTING_NULL_PROPERTIES_NOT_ALLOWED);
                }

                byte[] src = kvp.getValue().getValueAsString().getBytes(Constants.UTF8_CHARSET);
                byte[] dest = myAes.doFinal(src, 0, src.length);

                // Store the encrypted properties as binary values on the service instead of base 64 encoded strings because strings are stored as a sequence of 
                // WCHARs thereby further reducing the allowed size by half. During retrieve, it is handled by the response parsers correctly 
                // even when the service does not return the type for JSON no-metadata.
                encryptedProperties.put(kvp.getKey(), new EntityProperty(dest));
                encryptionPropertyDetailsSet.add(kvp.getKey());
            } else {
                encryptedProperties.put(kvp.getKey(), kvp.getValue());
            }

            // Encrypt the property details set and add it to entity properties.
            byte[] metadataIVFull = digest.digest(Utility.binaryAppend(encryptionData.getContentEncryptionIV(),
                    (partitionKey + rowKey + Constants.EncryptionConstants.TABLE_ENCRYPTION_PROPERTY_DETAILS)
                            .getBytes(Constants.UTF8_CHARSET)));

            byte[] metadataIV = new byte[16];
            System.arraycopy(metadataIVFull, 0, metadataIV, 0, 16);
            myAes.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(metadataIV));

            byte[] src = Arrays.toString(encryptionPropertyDetailsSet.toArray())
                    .getBytes(Constants.UTF8_CHARSET);
            byte[] dest = myAes.doFinal(src, 0, src.length);
            encryptedProperties.put(Constants.EncryptionConstants.TABLE_ENCRYPTION_PROPERTY_DETAILS,
                    new EntityProperty(dest));
        }

        encryptedProperties.put(Constants.EncryptionConstants.TABLE_ENCRYPTION_KEY_DETAILS,
                new EntityProperty(encryptionData.serialize()));

        return encryptedProperties;
    } catch (Exception e) {
        throw StorageException.translateClientException(e);
    }
}

From source file:org.tellervo.desktop.bulkdataentry.command.ImportSelectedObjectsCommand.java

/**
 * @see com.dmurph.mvc.control.ICommand#execute(com.dmurph.mvc.MVCEvent)
 *///from   w ww.j  a  v a2 s  .c  om
@Override
public void execute(MVCEvent argEvent) {
    BulkImportModel model = BulkImportModel.getInstance();

    ObjectTableModel tmodel = model.getObjectModel().getTableModel();
    ArrayList<IBulkImportSingleRowModel> selected = new ArrayList<IBulkImportSingleRowModel>();
    tmodel.getSelected(selected);

    // here is where we verify they contain required info
    HashSet<String> requiredMessages = new HashSet<String>();
    ArrayList<IBulkImportSingleRowModel> incompleteModels = new ArrayList<IBulkImportSingleRowModel>();

    HashSet<String> definedProps = new HashSet<String>();
    HashSet<String> objectCodeSet = new HashSet<String>();
    for (IBulkImportSingleRowModel som : selected) {

        definedProps.clear();
        for (String s : SingleObjectModel.TABLE_PROPERTIES) {
            if (som.getProperty(s) != null) {
                definedProps.add(s);
            }
        }
        boolean incomplete = false;

        // object code
        if (!definedProps.contains(SingleObjectModel.OBJECT_CODE)) {
            requiredMessages.add("Cannot import without an object code.");
            incomplete = true;
        } else {
            String code = som.getProperty(SingleObjectModel.OBJECT_CODE).toString();
            if (code.length() < 3) {
                requiredMessages.add("Object code must be at least 3 characters");
                incomplete = true;
            }
            if (code.contains(" ")) {
                requiredMessages.add("Object code cannot contain whitespace.");
                incomplete = true;
            }
            if (objectCodeSet.contains(code)) {
                requiredMessages.add("There cannot be duplicate object codes.");
                incomplete = true;
            } else {
                objectCodeSet.add(code);
            }
        }

        if (definedProps.contains(SingleObjectModel.PARENT_OBJECT)) {
            if (this.fixTempObjectCode(som)) {
                // fixed
            } else {
                requiredMessages.add("Cannot import as parent object has not been created yet");
                incomplete = true;
            }
        }

        // type
        if (!definedProps.contains(SingleObjectModel.TYPE)) {
            requiredMessages.add("Object must contain type.");
            incomplete = true;
        }

        // title
        if (!definedProps.contains(SingleObjectModel.TITLE)) {
            requiredMessages.add("Object must have a title");
            incomplete = true;
        }

        // lat/long
        if (definedProps.contains(SingleObjectModel.LATITUDE)
                || definedProps.contains(SingleObjectModel.LONGITUDE)) {
            if (!definedProps.contains(SingleObjectModel.LATITUDE)
                    || !definedProps.contains(SingleObjectModel.LONGITUDE)) {
                requiredMessages
                        .add("If coordinates are specified then both latitude and longitude are required");
                incomplete = true;
            } else {
                String attempt = som.getProperty(SingleObjectModel.LATITUDE).toString().trim();
                try {
                    Double lat = Double.parseDouble(attempt);
                    if (lat > -90 || lat < 90) {
                        requiredMessages
                                .add("Latitude must be betweOne or more errors were encountereden -90 and 90");
                        incomplete = true;
                    }
                } catch (NumberFormatException e) {
                    requiredMessages.add("Cannot parse '" + attempt + "' into a number.");
                    incomplete = true;
                }
                attempt = som.getProperty(SingleObjectModel.LONGITUDE).toString().trim();
                try {
                    Double lng = Double.parseDouble(attempt);
                    if (lng > -180 || lng < 180) {
                        requiredMessages.add("Longitude must be between -180 and 180");
                        incomplete = true;
                    }
                } catch (NumberFormatException e) {
                    requiredMessages.add("Cannot parse '" + attempt + "' into a number.");
                    incomplete = true;
                }
            }
        }

        if (incomplete) {
            incompleteModels.add(som);
        }
    }

    if (!incompleteModels.isEmpty()) {
        StringBuilder message = new StringBuilder();
        message.append("Please correct the following errors:\n");
        message.append(StringUtils.join(requiredMessages.toArray(), "\n"));
        Alert.message(model.getMainView(), "Importing Errors", message.toString());
        return;
    }

    // now we actually create the models
    int i = 0;
    for (IBulkImportSingleRowModel srm : selected) {
        SingleObjectModel som = (SingleObjectModel) srm;
        TridasObjectEx origObject = new TridasObjectEx();

        if (!som.isDirty()) {
            System.out.println("Object isn't dirty, not saving/updating: "
                    + som.getProperty(SingleObjectModel.OBJECT_CODE).toString());
        }

        som.populateTridasObject(origObject);

        TridasObject parentObject = null;
        try {
            parentObject = ((TridasObjectOrPlaceholder) som.getProperty(SingleObjectModel.PARENT_OBJECT))
                    .getTridasObject();
        } catch (Exception e) {

        }

        EntityResource<TridasObjectEx> resource;
        if (origObject.getIdentifier() != null) {
            resource = new EntityResource<TridasObjectEx>(origObject, TellervoRequestType.UPDATE,
                    TridasObjectEx.class);
        } else {
            if (parentObject != null) {
                resource = new EntityResource<TridasObjectEx>(origObject, parentObject, TridasObjectEx.class);
            } else {
                resource = new EntityResource<TridasObjectEx>(origObject, TellervoRequestType.CREATE,
                        TridasObjectEx.class);
            }
        }

        // set up a dialog...
        Window parentWindow = SwingUtilities.getWindowAncestor(model.getMainView());
        TellervoResourceAccessDialog dialog = new TellervoResourceAccessDialog(parentWindow, resource, i,
                selected.size());

        resource.query();
        dialog.setVisible(true);

        if (!dialog.isSuccessful()) {
            JOptionPane.showMessageDialog(BulkImportModel.getInstance().getMainView(),
                    I18n.getText("error.savingChanges") + "\r\n" + I18n.getText("error") + ": "
                            + dialog.getFailException().getLocalizedMessage(),
                    I18n.getText("error"), JOptionPane.ERROR_MESSAGE);
            continue;
        }
        som.populateFromTridasObject(resource.getAssociatedResult());
        som.setDirty(false);
        tmodel.setSelected(som, false);

        // add to imported list or update existing
        if (origObject.getIdentifier() != null) {
            TridasObjectEx found = null;
            for (TridasObjectEx tox : model.getObjectModel().getImportedList()) {
                if (tox.getIdentifier().getValue().equals(origObject.getIdentifier().getValue())) {
                    found = tox;
                    break;
                }
            }
            if (found == null) {
                Alert.error("Error updating model",
                        "Couldn't find the object in the model to update, please report bug.");
            } else {
                resource.getAssociatedResult().copyTo(found);
                App.tridasObjects.updateTridasObject(found);
            }
        } else {
            model.getObjectModel().getImportedList().add(resource.getAssociatedResult());
            App.tridasObjects.addTridasObject(resource.getAssociatedResult());
        }

        i++;
    }

    // finally, update the combo boxes in the table to the new options
    //DynamicJComboBoxEvent event = new DynamicJComboBoxEvent(model.getObjectModel().getImportedDynamicComboBoxKey(), model.getObjectModel().getImportedListStrings());
    //event.dispatch();

    // FIXME this should be removed once other lists listen for changes in the object list
    //App.updateTridasObjectList();

}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void ortest3() {
    final HashSet<Integer> V1 = new HashSet<Integer>();
    final HashSet<Integer> V2 = new HashSet<Integer>();

    final RoaringBitmap rr = new RoaringBitmap();
    final RoaringBitmap rr2 = new RoaringBitmap();
    // For the first 65536: rr2 has a bitmap container, and rr has
    // an array container.
    // We will check the union between a BitmapCintainer and an
    // arrayContainer
    for (int k = 0; k < 4000; ++k) {
        rr2.add(k);/* w w  w. jav  a 2s  .c  om*/
        V1.add(k);
    }
    for (int k = 3500; k < 4500; ++k) {
        rr.add(k);
        V1.add(k);
    }
    for (int k = 4000; k < 65000; ++k) {
        rr2.add(k);
        V1.add(k);
    }

    // In the second node of each roaring bitmap, we have two bitmap
    // containers.
    // So, we will check the union between two BitmapContainers
    for (int k = 65536; k < 65536 + 10000; ++k) {
        rr.add(k);
        V1.add(k);
    }

    for (int k = 65536; k < 65536 + 14000; ++k) {
        rr2.add(k);
        V1.add(k);
    }

    // In the 3rd node of each Roaring Bitmap, we have an
    // ArrayContainer, so, we will try the union between two
    // ArrayContainers.
    for (int k = 4 * 65535; k < 4 * 65535 + 1000; ++k) {
        rr.add(k);
        V1.add(k);
    }

    for (int k = 4 * 65535; k < 4 * 65535 + 800; ++k) {
        rr2.add(k);
        V1.add(k);
    }

    // For the rest, we will check if the union will take them in
    // the result
    for (int k = 6 * 65535; k < 6 * 65535 + 1000; ++k) {
        rr.add(k);
        V1.add(k);
    }

    for (int k = 7 * 65535; k < 7 * 65535 + 2000; ++k) {
        rr2.add(k);
        V1.add(k);
    }

    final RoaringBitmap rror = RoaringBitmap.or(rr, rr2);
    boolean valide = true;

    // Si tous les elements de rror sont dans V1 et que tous les
    // elements de
    // V1 sont dans rror(V2)
    // alors V1 == rror

    final Object[] tab = V1.toArray();
    final Vector<Integer> vector = new Vector<Integer>();
    for (Object aTab : tab)
        vector.add((Integer) aTab);

    for (final int i : rror.toArray()) {
        if (!vector.contains(new Integer(i))) {
            valide = false;
        }
        V2.add(i);
    }
    for (int i = 0; i < V1.size(); i++)
        if (!V2.contains(vector.elementAt(i))) {
            valide = false;
        }

    Assert.assertEquals(valide, true);
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

@Test
public void xortest1() {
    final HashSet<Integer> V1 = new HashSet<Integer>();
    final HashSet<Integer> V2 = new HashSet<Integer>();

    final RoaringBitmap rr = new RoaringBitmap();
    final RoaringBitmap rr2 = new RoaringBitmap();
    // For the first 65536: rr2 has a bitmap container, and rr has
    // an array container.
    // We will check the union between a BitmapCintainer and an
    // arrayContainer
    for (int k = 0; k < 4000; ++k) {
        rr2.add(k);//w  w w  . ja  v  a2s  . c  om
        if (k < 3500)
            V1.add(k);
    }
    for (int k = 3500; k < 4500; ++k) {
        rr.add(k);
    }
    for (int k = 4000; k < 65000; ++k) {
        rr2.add(k);
        if (k >= 4500)
            V1.add(k);
    }

    // In the second node of each roaring bitmap, we have two bitmap
    // containers.
    // So, we will check the union between two BitmapContainers
    for (int k = 65536; k < 65536 + 30000; ++k) {
        rr.add(k);
    }

    for (int k = 65536; k < 65536 + 50000; ++k) {
        rr2.add(k);
        if (k >= 65536 + 30000)
            V1.add(k);
    }

    // In the 3rd node of each Roaring Bitmap, we have an
    // ArrayContainer. So, we will try the union between two
    // ArrayContainers.
    for (int k = 4 * 65535; k < 4 * 65535 + 1000; ++k) {
        rr.add(k);
        if (k >= 4 * 65535 + 800)
            V1.add(k);
    }

    for (int k = 4 * 65535; k < 4 * 65535 + 800; ++k) {
        rr2.add(k);
    }

    // For the rest, we will check if the union will take them in
    // the result
    for (int k = 6 * 65535; k < 6 * 65535 + 1000; ++k) {
        rr.add(k);
        V1.add(k);
    }

    for (int k = 7 * 65535; k < 7 * 65535 + 2000; ++k) {
        rr2.add(k);
        V1.add(k);
    }

    final RoaringBitmap rrxor = RoaringBitmap.xor(rr, rr2);
    boolean valide = true;

    // Si tous les elements de rror sont dans V1 et que tous les
    // elements de
    // V1 sont dans rror(V2)
    // alors V1 == rror
    final Object[] tab = V1.toArray();
    final Vector<Integer> vector = new Vector<Integer>();
    for (Object aTab : tab)
        vector.add((Integer) aTab);

    for (final int i : rrxor.toArray()) {
        if (!vector.contains(new Integer(i))) {
            valide = false;
        }
        V2.add(i);
    }
    for (int i = 0; i < V1.size(); i++)
        if (!V2.contains(vector.elementAt(i))) {
            valide = false;
        }

    Assert.assertEquals(valide, true);
}

From source file:org.tellervo.desktop.bulkdataentry.command.ImportSelectedElementsCommand.java

/**
 * @see com.dmurph.mvc.control.ICommand#execute(com.dmurph.mvc.MVCEvent)
 *//* w w w  .  ja  va2s .  c  om*/
@Override
public void execute(MVCEvent argEvent) {
    BulkImportModel model = BulkImportModel.getInstance();
    ElementModel emodel = model.getElementModel();
    ElementTableModel tmodel = emodel.getTableModel();

    ArrayList<IBulkImportSingleRowModel> selected = new ArrayList<IBulkImportSingleRowModel>();
    tmodel.getSelected(selected);

    // here is where we verify they contain required info
    HashSet<String> requiredMessages = new HashSet<String>();
    ArrayList<IBulkImportSingleRowModel> incompleteModels = new ArrayList<IBulkImportSingleRowModel>();

    HashSet<String> definedProps = new HashSet<String>();
    for (IBulkImportSingleRowModel som : selected) {

        definedProps.clear();
        for (String s : SingleElementModel.TABLE_PROPERTIES) {
            if (som.getProperty(s) != null) {
                definedProps.add(s);
            }
        }
        boolean incomplete = false;

        // object 
        if (!definedProps.contains(SingleElementModel.OBJECT)) {
            requiredMessages.add("Cannot import without a parent object.");
            incomplete = true;
        } else if (fixTempObjectCode(som)) {
            // There was a temp code but it is fixed now
        } else {
            requiredMessages.add("Cannot import as parent object has not been created yet");
            incomplete = true;
        }

        // type
        if (!definedProps.contains(SingleElementModel.TYPE)) {
            requiredMessages.add("Element must contain a type.");
            incomplete = true;
        }

        // taxon
        if (!definedProps.contains(SingleElementModel.TAXON)) {
            requiredMessages.add("Element must contain a taxon.");
            incomplete = true;
        }

        // title
        if (!definedProps.contains(SingleElementModel.TITLE)) {
            requiredMessages.add("Element must have a title");
            incomplete = true;
        }

        // lat/long
        if (definedProps.contains(SingleElementModel.LATITUDE)
                || definedProps.contains(SingleElementModel.LONGITUDE)) {
            if (!definedProps.contains(SingleElementModel.LATITUDE)
                    || !definedProps.contains(SingleElementModel.LONGITUDE)) {
                requiredMessages
                        .add("If coordinates are specified then both latitude and longitude are required");
                incomplete = true;
            } else {
                String attempt = som.getProperty(SingleElementModel.LATITUDE).toString().trim();
                try {
                    Double lat = Double.parseDouble(attempt);
                    if (lat > -90 || lat < 90) {
                        requiredMessages.add("Latitude must be between -90 and 90");
                        incomplete = true;
                    }
                } catch (NumberFormatException e) {
                    requiredMessages.add("Cannot parse '" + attempt + "' into a number.");
                    incomplete = true;
                }
                attempt = som.getProperty(SingleElementModel.LONGITUDE).toString().trim();
                try {
                    Double lng = Double.parseDouble(attempt);
                    if (lng > -180 || lng < 180) {
                        requiredMessages.add("Longitude must be between -180 and 180");
                        incomplete = true;
                    }
                } catch (NumberFormatException e) {
                    requiredMessages.add("Cannot parse '" + attempt + "' into a number.");
                    incomplete = true;
                }
            }
        }

        if (definedProps.contains(SingleElementModel.HEIGHT) || definedProps.contains(SingleElementModel.WIDTH)
                || definedProps.contains(SingleElementModel.DEPTH)
                || definedProps.contains(SingleElementModel.DIAMETER)) {
            if (!definedProps.contains(SingleElementModel.UNIT)) {
                requiredMessages.add("Units must be specified when dimensions are included");
                incomplete = true;
            }

            if ((definedProps.contains(SingleElementModel.HEIGHT)
                    && definedProps.contains(SingleElementModel.DIAMETER)
                    && !definedProps.contains(SingleElementModel.WIDTH)
                    && !definedProps.contains(SingleElementModel.DEPTH))) {
                // h+diam but not width or depth
            } else if ((definedProps.contains(SingleElementModel.HEIGHT)
                    && definedProps.contains(SingleElementModel.WIDTH)
                    && definedProps.contains(SingleElementModel.DEPTH)
                    && !definedProps.contains(SingleElementModel.DIAMETER))) {
                // h+w+d but not diam
            } else {
                requiredMessages.add(
                        "When dimensions are included they must be: height/width/depth or height/diameter.");
                incomplete = true;
            }

        }

        if (incomplete) {
            incompleteModels.add(som);
        }
    }

    if (!incompleteModels.isEmpty()) {
        StringBuilder message = new StringBuilder();
        message.append("Please correct the following errors:\n");
        message.append(StringUtils.join(requiredMessages.toArray(), "\n"));
        Alert.message(model.getMainView(), "Importing Errors", message.toString());
        return;
    }

    // now we actually create the models
    int i = -1;
    boolean hideErrorMessage = false;
    for (IBulkImportSingleRowModel srm : selected) {
        i++;
        SingleElementModel som = (SingleElementModel) srm;
        TridasElement origElement = new TridasElement();

        if (!som.isDirty()) {
            System.out.println("Element isn't dirty, not saving/updating: "
                    + som.getProperty(SingleElementModel.TITLE).toString());
        }

        som.populateToTridasElement(origElement);

        Object o = som.getProperty(SingleElementModel.OBJECT);
        TridasObject parentObject = null;
        if (o instanceof TridasObjectOrPlaceholder) {
            parentObject = ((TridasObjectOrPlaceholder) o).getTridasObject();
        } else if (o instanceof TridasObject) {
            parentObject = (TridasObject) o;
        }

        EntityResource<TridasElement> resource;

        if (origElement.getIdentifier() != null) {
            resource = new EntityResource<TridasElement>(origElement, TellervoRequestType.UPDATE,
                    TridasElement.class);
        } else {
            resource = new EntityResource<TridasElement>(origElement, parentObject, TridasElement.class);
        }

        // set up a dialog...
        Window parentWindow = SwingUtilities.getWindowAncestor(model.getMainView());
        TellervoResourceAccessDialog dialog = new TellervoResourceAccessDialog(parentWindow, resource, i,
                selected.size());

        resource.query();
        dialog.setVisible(true);

        if (!dialog.isSuccessful()) {

            if (hideErrorMessage) {
                continue;
            } else if (i < selected.size() - 1) {
                // More records remain
                Object[] options = { "Yes", "Yes, but hide further messages", "No" };
                int result = JOptionPane.showOptionDialog(BulkImportModel.getInstance().getMainView(), //parent
                        I18n.getText("error.savingChanges") + ":" + System.lineSeparator() + // message
                                dialog.getFailException().getLocalizedMessage() + System.lineSeparator()
                                + System.lineSeparator()
                                + "Would you like to continue importing the remaining records?",
                        I18n.getText("error"), // title 
                        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);

                if (result == JOptionPane.NO_OPTION) {
                    hideErrorMessage = true;
                    continue;
                } else if (result == JOptionPane.YES_OPTION) {
                    continue;
                } else {
                    break;
                }
            } else {
                JOptionPane.showMessageDialog(BulkImportModel.getInstance().getMainView(), //parent
                        I18n.getText("error.savingChanges") + ":" + System.lineSeparator() + // message
                                dialog.getFailException().getLocalizedMessage(),
                        I18n.getText("error"), // title 
                        JOptionPane.ERROR_MESSAGE); //option

                break;
            }
        }
        som.populateFromTridasElement(resource.getAssociatedResult());
        som.setDirty(false);
        tmodel.setSelected(som, false);

        // add to imported list or update existing
        if (origElement.getIdentifier() != null) {
            TridasElement found = null;
            for (TridasElement tox : model.getElementModel().getImportedList()) {
                if (tox.getIdentifier().getValue().equals(origElement.getIdentifier().getValue())) {
                    found = tox;
                    break;
                }
            }
            if (found == null) {
                log.warn(
                        "Error updating model.  Couldn't find the object in the model to update so adding to imported list.");
                model.getElementModel().getImportedList().add(resource.getAssociatedResult());
            } else {
                resource.getAssociatedResult().copyTo(found);
            }
        } else {
            model.getElementModel().getImportedList().add(resource.getAssociatedResult());
        }

    }

    //      
    //      // finally, update the combo boxes in the table to the new options
    //      DynamicJComboBoxEvent event = new DynamicJComboBoxEvent(emodel.getImportedDynamicComboBoxKey(), emodel.getImportedListStrings());
    //      event.dispatch();

    tmodel.fireTableDataChanged();
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jBochsCommandTextFieldKeyPressed(KeyEvent evt) {
    if (bochsCommandTextField.getText().equals("")) {
        commandHistoryIndex = 0;/*from www  .j av  a2  s  .  co m*/
    }
    HashSet<String> vector = Setting.getInstance().getBochsCommandHistory();
    if (evt.getKeyCode() == 38) {
        if (commandHistoryIndex < vector.size()) {
            commandHistoryIndex++;
            this.bochsCommandTextField
                    .setText(vector.toArray()[vector.size() - commandHistoryIndex].toString());
        }
    } else if (evt.getKeyCode() == 40) {
        if (commandHistoryIndex > 1) {
            commandHistoryIndex--;
            this.bochsCommandTextField
                    .setText(vector.toArray()[vector.size() - commandHistoryIndex].toString());
        }
    }
}