List of usage examples for org.apache.commons.lang3 ArrayUtils toPrimitive
public static boolean[] toPrimitive(final Boolean[] array)
Converts an array of object Booleans to primitives.
This method returns null for a null input array.
From source file:com.github.jessemull.microflex.math.mathinteger.MultiplicationIntegerTest.java
/** * Tests the strict multiplication of an array to a well using indices. *//*from ww w . j a v a2s . c om*/ @Test public void testWellStrictArrayIndices() { PlateInteger[] plates1 = arrays1.get(0); PlateInteger[] plates2 = arrays2.get(0); PlateInteger[] unevenPlates = uneven.get(0); for (int i = 0; i < plates1.length; i++) { WellInteger[] well1 = plates1[i].dataSet().toWellArray(); WellInteger[] well2 = plates2[i].dataSet().toWellArray(); WellInteger[] wellUneven = unevenPlates[i].dataSet().toWellArray(); for (int j = 0; j < well1.length; j++) { List<Integer> list1 = well1[j].data(); List<Integer> list2 = well2[j].data(); int[] array = ArrayUtils.toPrimitive(list2.toArray(new Integer[list2.size()])); List<Integer> unevenList = wellUneven[j].data(); int[] arrayUneven = ArrayUtils.toPrimitive(unevenList.toArray(new Integer[unevenList.size()])); int begin = 1 + random.nextInt(list1.size() - 1); int end = begin + random.nextInt(list1.size() - begin); List<Integer> result = new ArrayList<Integer>(); List<Integer> resultUneven = new ArrayList<Integer>(); List<Integer> returned = multiplication.wellsStrict(well1[j], array, begin, end - begin); List<Integer> returnedUneven = multiplication.wellsStrict(well1[j], arrayUneven, begin, end - begin); for (int k = begin; k < end; k++) { result.add(list1.get(k) * array[k]); resultUneven.add(list1.get(k) * arrayUneven[k]); } assertEquals(result, returned); assertEquals(resultUneven, returnedUneven); } } }
From source file:com.dotweblabs.twirl.gae.GaeMarshaller.java
/** * Get the <code>List</code> out of the Embedded entity. * The <code>List</code> is expected to be stored following a dot (.) notation. * E.g. A JSON array with a keyType of "numbers" will be stored as a <code>EmbeddedEntity</code> * with property names:// w w w . ja va2 s.c o m * * <code> * numbers.0 * numbers.1 * numbers.2 * </code> * * And so on. And since it is stored a a <code>EmbeddedEntity</code> then it is ambiguous to a * <code>Map</code> that is also stored in the same Datastore type. * * @param ee {@code EmbeddedEntity} to unmarshall * @return {@code List} of unmarshalled {@code EmbeddedEntity} */ private static List<Object> createListFromEmbeddedEntity(final EmbeddedEntity ee) { List<Object> list = null; Iterator<Map.Entry<String, Object>> it = ee.getProperties().entrySet().iterator(); Object[] arr = new Object[1024]; List<Integer> indexToRemove = new ArrayList<Integer>(); for (int i = 0; i < arr.length; i++) { indexToRemove.add(i); } while (it.hasNext()) { Map.Entry<String, Object> entry = it.next(); try { if (list == null) { list = new LinkedList<Object>(); } Object value = entry.getValue(); Integer i = Integer.valueOf(entry.getKey()); LOG.debug("Value=" + entry.getValue()); if (value instanceof String || value instanceof Boolean || value instanceof Number || value instanceof Date || value instanceof User) // GAE supported type { arr[i] = value; indexToRemove.remove(i); } else if (value instanceof EmbeddedEntity) { arr[i] = getMapOrList((EmbeddedEntity) value); indexToRemove.remove(i); } else { throw new RuntimeException("Invalid JSON field type in embedded list entity"); } } catch (Exception e) { // TODO Handle exception e.printStackTrace(); } } int[] intArray = ArrayUtils.toPrimitive(indexToRemove.toArray(new Integer[indexToRemove.size()])); arr = copyArrayRemove(arr, intArray); return Arrays.asList(arr); }
From source file:com.rockhoppertech.music.chord.ChordFactory.java
public static Chord[] getChords(Scale scale, boolean seven, boolean nine, boolean eleven, boolean thirteen) { int[] intervals = scale.getIntervals(); Chord[] chords = new Chord[intervals.length + 1]; // e.g. if you are on the fifth degree and you // want a triad you have already gone past the end of the intervals. // so extend them. int[] intervalsX2 = ArrayFunctions.appendCopy(intervals); intervalsX2 = ArrayFunctions.appendCopy(intervalsX2); List<Integer> intervalList = new LinkedList<Integer>(); for (int degree = 0; degree < scale.getLength(); degree++) { int chordroot = scale.getDegree(degree); int third = ArrayFunctions.sum(intervalsX2, degree, degree + 1); int fifth = ArrayFunctions.sum(intervalsX2, degree, degree + 3); intervalList.add(third);// w w w.ja v a 2s . c om intervalList.add(fifth); logger.debug(String.format("chordroot:%d root:%d third:%d fifth:%d", chordroot, degree, third, fifth)); logger.debug("degree:" + degree); logger.debug("chordroot:" + PitchFactory.getPitch(chordroot)); logger.debug("third:" + PitchFactory.getPitch(chordroot + third)); logger.debug("fifth:" + PitchFactory.getPitch(chordroot + fifth)); if (seven) { int seventh = ArrayFunctions.sum(intervalsX2, degree, degree + 5); intervalList.add(seventh); logger.debug("seventh:" + PitchFactory.getPitch(chordroot + seventh)); } if (nine) { int ninth = ArrayFunctions.sum(intervalsX2, degree, degree + 7); intervalList.add(ninth); logger.debug("ninth:" + PitchFactory.getPitch(chordroot + ninth)); } if (eleven) { int eleventh = ArrayFunctions.sum(intervalsX2, degree, degree + 9); intervalList.add(eleventh); logger.debug("eleventh:" + PitchFactory.getPitch(chordroot + eleventh)); } if (thirteen) { int thirteenth = ArrayFunctions.sum(intervalsX2, degree, degree + 11); intervalList.add(thirteenth); logger.debug("thirteenth:" + PitchFactory.getPitch(chordroot + thirteenth)); } Integer[] ints = new Integer[intervalList.size()]; ints = intervalList.toArray(ints); int[] someIntervals = ArrayUtils.toPrimitive(ints); logger.debug(ArrayUtils.toString(someIntervals)); intervalList.clear(); try { Chord c = createFromIntervals(someIntervals); c.setRoot(chordroot); chords[degree] = c; } catch (UnknownChordException e) { logger.debug(String.format("%s-%d", scale.getName(), chordroot)); for (int i : someIntervals) { logger.debug("interval {}", PitchFactory.getPitch(chordroot + i)); } Chord mc = new Chord(String.format("wtf-%s-d%d-%b-%b-%b-%b", scale.getName(), degree, seven, nine, eleven, thirteen), intervals, "unregistered chord"); ChordFactory.registerChord(mc, mc.getSymbol()); mc.setRoot(chordroot); chords[degree] = mc; } catch (Exception e) { logger.error(e.getLocalizedMessage(), e); } } return chords; }
From source file:com.andrew.apollo.utils.MusicUtils.java
private static long[] getSongListForAdapter(final ArrayAdapter<Song> adapter) { if (adapter == null) { return sEmptyList; }/*from w ww . j av a2s .co m*/ int count = adapter.getCount() - (adapter.getViewTypeCount() > 1 ? 1 : 0); List<Long> songList = new LinkedList<>(); for (int i = 0; i < count; i++) { try { long songId = adapter.getItem(i).mSongId; songList.add(songId); } catch (Throwable ignored) { // possible array out of bounds on adapter.getItem(i) } } if (songList.size() == 0) { return sEmptyList; } // until Java supports primitive types as generics, we'll live with this double copy. O(2n) Long[] list = new Long[songList.size()]; long[] result = ArrayUtils.toPrimitive(songList.toArray(list)); songList.clear(); return result; }
From source file:corelyzer.ui.CorelyzerGLCanvas.java
private void updateMainFrameListSelection(final int track, final int section, final MouseEvent event) { CorelyzerApp app = CorelyzerApp.getApp(); if (app == null) { return;/*from w ww . jav a 2 s. co m*/ } if (track >= 0) { // Now, we need to traverse app's list model // to find match of native id // index conversion (native to java list) CRDefaultListModel sessionModel = app.getSessionListModel(); int sessionIndex = -1; TrackSceneNode trackNode = null; for (int i = 0; i < sessionModel.size(); i++) { Session session = (Session) sessionModel.elementAt(i); trackNode = session.getTrackSceneNodeWithTrackId(track); if (trackNode != null) { sessionIndex = i; } } if (sessionIndex < 0) { return; } // Set selected session app.getSessionList().setSelectedIndex(sessionIndex); // Track int ssize; boolean found = false; CRDefaultListModel tmodel = app.getTrackListModel(); // tsize = tmodel.getSize(); TrackSceneNode tt; CoreSection cs = null; for (int i = 0; i < tmodel.size() && !found; i++) { tt = (TrackSceneNode) tmodel.elementAt(i); if (track == tt.getId()) { selectedTrackIndex = i; ssize = tt.getNumCores(); for (int j = 0; j < ssize; j++) { cs = tt.getCoreSection(j); if (section == cs.getId()) { selectedTrackSectionIndex = j; found = true; break; } } } } if (!found || cs == null) { return; } // update ui CorelyzerApp.getApp().getTrackList().setSelectedIndex(selectedTrackIndex); JList secList = CorelyzerApp.getApp().getSectionList(); boolean selected = secList.isSelectedIndex(selectedTrackSectionIndex); List<Integer> indices = new ArrayList<Integer>(); indices.addAll(Arrays.asList(ArrayUtils.toObject(secList.getSelectedIndices()))); if (event.isControlDown() || (event.isMetaDown() && CorelyzerApp.MAC_OS_X)) { // toggle selection if (indices.contains(selectedTrackSectionIndex)) indices.remove(new Integer(selectedTrackSectionIndex)); else indices.add(selectedTrackSectionIndex); int[] newSelArray = ArrayUtils.toPrimitive(indices.toArray(new Integer[0])); secList.setSelectedIndices(newSelArray); } else if (event.isShiftDown()) { // select range int[] toSel = null; if (indices.size() == 0) { toSel = makeRangeArray(0, selectedTrackSectionIndex); } else { final int minSel = Collections.min(indices); final int maxSel = Collections.max(indices); if (selectedTrackSectionIndex < minSel) { toSel = makeRangeArray(selectedTrackSectionIndex, minSel); } else if (selectedTrackSectionIndex > maxSel) { toSel = makeRangeArray(maxSel, selectedTrackSectionIndex); } } secList.setSelectedIndices(toSel); } else if (!(event.isAltDown() && selected)) { // don't modify selection if Alt is down and section was already // selected...user is presumably trying to move it secList.setSelectedIndex(selectedTrackSectionIndex); } CRDefaultListModel lm = CorelyzerApp.getApp().getSectionListModel(); String secName = null; if (lm != null) { Object selSec = lm.getElementAt(selectedTrackSectionIndex); if (selSec != null) { secName = selSec.toString(); } else { System.out.println("no object at index"); } } else { System.out.println("no list model"); } JMenuItem title = (JMenuItem) this.scenePopupMenu.getComponent(0); String trackName = CorelyzerApp.getApp().getTrackListModel().getElementAt(selectedTrackIndex) .toString(); title.setText("Track: " + trackName); JMenuItem stitle = (JMenuItem) this.scenePopupMenu.getComponent(1); stitle.setText("Section: " + secName); // Enable section-based popupMenu options this.setEnableSectionBasedPopupMenuOptions(true); // 2/5/2012 brg: check Stagger Sections menu item if necessary final boolean trackIsStaggered = SceneGraph.trackIsStaggered(selectedTrack); AbstractButton ab = (AbstractButton) this.scenePopupMenu.getComponent(14); ab.getModel().setSelected(trackIsStaggered); // check section and graph lock menu items final boolean sectionIsLocked = !SceneGraph.isSectionMovable(selectedTrack, selectedTrackSection); ab = (AbstractButton) this.scenePopupMenu.getComponent(7); ab.getModel().setSelected(sectionIsLocked); final boolean sectionGraphIsLocked = !SceneGraph.isSectionGraphMovable(selectedTrack, selectedTrackSection); ab = (AbstractButton) this.scenePopupMenu.getComponent(8); ab.getModel().setSelected(sectionGraphIsLocked); CoreSectionImage csImg = cs.getCoreSectionImage(); if (csImg != null && csImg.getId() != -1) { this.propertyMenuItem.setEnabled(true); this.splitMenuItem.setEnabled(true); } else { this.propertyMenuItem.setEnabled(false); this.splitMenuItem.setEnabled(false); } // System.out.println("---> [in Java DS] Picked Track " + track + // " and Track Section " + section); // String secname = CorelyzerApp.getApp().getSectionListModel(). // getElementAt(section).toString(); // System.out.println("---> [INFO] Section " + secname + // " is selected"); } else { JMenuItem title = (JMenuItem) this.scenePopupMenu.getComponent(0); title.setText("Track: N/A"); JMenuItem stitle = (JMenuItem) this.scenePopupMenu.getComponent(1); stitle.setText("Section: N/A"); // disable section based items this.setEnableSectionBasedPopupMenuOptions(false); } }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.hplus.HPlusSupport.java
/** * HPlus devices accept a subset of GB2312 with some modifications. * This function will apply a custom transliteration. * While related to the methods implemented in LanguageUtils. These are specific for HPLUS * * @param s The String to transliterate//w w w .ja v a2 s .co m * @return An array of bytes ready to be sent to the device */ private byte[] encodeStringToDevice(String s) { List<Byte> outBytes = new ArrayList<Byte>(); for (int i = 0; i < s.length(); i++) { Character c = s.charAt(i); byte[] cs; if (HPlusConstants.transliterateMap.containsKey(c)) { cs = new byte[] { HPlusConstants.transliterateMap.get(c) }; } else { try { cs = c.toString().getBytes("GB2312"); } catch (UnsupportedEncodingException e) { //Fallback. Result string may be strange, but better than nothing cs = c.toString().getBytes(); } } for (int j = 0; j < cs.length; j++) outBytes.add(cs[j]); } return ArrayUtils.toPrimitive(outBytes.toArray(new Byte[outBytes.size()])); }
From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java
@VisibleForTesting static int[] toIntPortArray(Object v) { PortRange portRange = PortRanges.fromIterable(Collections.singletonList(v)); int[] portArray = ArrayUtils.toPrimitive(Iterables.toArray(portRange, Integer.class)); return portArray; }
From source file:org.apache.brooklyn.location.jclouds.templates.customize.InboundPortsOption.java
private int[] toIntPortArray(Object v) { PortRange portRange = PortRanges.fromIterable(Collections.singletonList(v)); return ArrayUtils.toPrimitive(Iterables.toArray(portRange, Integer.class)); }
From source file:org.apache.carbondata.core.carbon.datastore.block.SegmentProperties.java
/** * Below method will fill the key generator detail of both the type of key * generator. This will be required for during both query execution and data * loading.//from w w w. j a va 2s. c o m */ private void fillKeyGeneratorDetails() { // create a dimension partitioner list // this list will contain information about how dimension value are // stored // it is stored in group or individually List<Integer> dimensionPartitionList = new ArrayList<Integer>( CarbonCommonConstants.DEFAULT_COLLECTION_SIZE); List<Boolean> isDictionaryColumn = new ArrayList<Boolean>(CarbonCommonConstants.DEFAULT_COLLECTION_SIZE); int prvcolumnGroupId = -1; int counter = 0; while (counter < dimensions.size()) { CarbonDimension carbonDimension = dimensions.get(counter); // if dimension is not a part of mdkey then no need to add if (!carbonDimension.getEncoder().contains(Encoding.DICTIONARY)) { isDictionaryColumn.add(false); counter++; continue; } // columnar column is stored individually // so add one if (carbonDimension.isColumnar()) { dimensionPartitionList.add(1); isDictionaryColumn.add(true); } // if in a group then need to add how many columns a selected in // group if (!carbonDimension.isColumnar() && carbonDimension.columnGroupId() == prvcolumnGroupId) { // incrementing the previous value of the list as it is in same column group dimensionPartitionList.set(dimensionPartitionList.size() - 1, dimensionPartitionList.get(dimensionPartitionList.size() - 1) + 1); } else if (!carbonDimension.isColumnar()) { dimensionPartitionList.add(1); isDictionaryColumn.add(true); } prvcolumnGroupId = carbonDimension.columnGroupId(); counter++; } // get the partitioner int[] dimensionPartitions = ArrayUtils .toPrimitive(dimensionPartitionList.toArray(new Integer[dimensionPartitionList.size()])); // get the bit length of each column int[] bitLength = CarbonUtil.getDimensionBitLength(dimColumnsCardinality, dimensionPartitions); // create a key generator this.dimensionKeyGenerator = new MultiDimKeyVarLengthGenerator(bitLength); this.fixedLengthKeySplitter = new MultiDimKeyVarLengthVariableSplitGenerator(bitLength, dimensionPartitions); // get the size of each value in file block int[] dictionayDimColumnValueSize = fixedLengthKeySplitter.getBlockKeySize(); int index = -1; this.eachDimColumnValueSize = new int[isDictionaryColumn.size()]; for (int i = 0; i < eachDimColumnValueSize.length; i++) { if (!isDictionaryColumn.get(i)) { eachDimColumnValueSize[i] = -1; continue; } eachDimColumnValueSize[i] = dictionayDimColumnValueSize[++index]; } if (complexDimensions.size() > 0) { int[] complexDimesionParition = new int[complexDimColumnCardinality.length]; // as complex dimension will be stored in column format add one Arrays.fill(complexDimesionParition, 1); bitLength = CarbonUtil.getDimensionBitLength(complexDimColumnCardinality, complexDimesionParition); for (int i = 0; i < bitLength.length; i++) { if (complexDimColumnCardinality[i] == 0) { bitLength[i] = 64; } } ColumnarSplitter keySplitter = new MultiDimKeyVarLengthVariableSplitGenerator(bitLength, complexDimesionParition); eachComplexDimColumnValueSize = keySplitter.getBlockKeySize(); } else { eachComplexDimColumnValueSize = new int[0]; } }
From source file:org.apache.carbondata.core.datastore.block.SegmentProperties.java
/** * Below method will fill the key generator detail of both the type of key * generator. This will be required for during both query execution and data * loading.// w w w.j a v a 2s . co m */ private void fillKeyGeneratorDetails() { // create a dimension partitioner list // this list will contain information about how dimension value are // stored // it is stored in group or individually List<Integer> dimensionPartitionList = new ArrayList<Integer>( CarbonCommonConstants.DEFAULT_COLLECTION_SIZE); List<Boolean> isDictionaryColumn = new ArrayList<Boolean>(CarbonCommonConstants.DEFAULT_COLLECTION_SIZE); int prvcolumnGroupId = -1; int counter = 0; while (counter < dimensions.size()) { CarbonDimension carbonDimension = dimensions.get(counter); // if dimension is not a part of mdkey then no need to add if (!carbonDimension.getEncoder().contains(Encoding.DICTIONARY)) { isDictionaryColumn.add(false); counter++; continue; } // columnar column is stored individually // so add one if (carbonDimension.isColumnar()) { dimensionPartitionList.add(1); isDictionaryColumn.add(true); } // if in a group then need to add how many columns a selected in // group if (!carbonDimension.isColumnar() && carbonDimension.columnGroupId() == prvcolumnGroupId) { // incrementing the previous value of the list as it is in same column group dimensionPartitionList.set(dimensionPartitionList.size() - 1, dimensionPartitionList.get(dimensionPartitionList.size() - 1) + 1); } else if (!carbonDimension.isColumnar()) { dimensionPartitionList.add(1); isDictionaryColumn.add(true); } prvcolumnGroupId = carbonDimension.columnGroupId(); counter++; } // get the partitioner dimensionPartitions = ArrayUtils .toPrimitive(dimensionPartitionList.toArray(new Integer[dimensionPartitionList.size()])); // get the bit length of each column int[] bitLength = CarbonUtil.getDimensionBitLength(dimColumnsCardinality, dimensionPartitions); // create a key generator this.dimensionKeyGenerator = new MultiDimKeyVarLengthGenerator(bitLength); if (this.getNumberOfDictSortColumns() == bitLength.length) { this.sortColumnsGenerator = this.dimensionKeyGenerator; } else { int numberOfDictSortColumns = this.getNumberOfDictSortColumns(); int[] sortColumnBitLength = new int[numberOfDictSortColumns]; System.arraycopy(bitLength, 0, sortColumnBitLength, 0, numberOfDictSortColumns); this.sortColumnsGenerator = new MultiDimKeyVarLengthGenerator(sortColumnBitLength); } this.fixedLengthKeySplitter = new MultiDimKeyVarLengthVariableSplitGenerator(bitLength, dimensionPartitions); // get the size of each value in file block int[] dictionayDimColumnValueSize = fixedLengthKeySplitter.getBlockKeySize(); int index = -1; this.eachDimColumnValueSize = new int[isDictionaryColumn.size()]; for (int i = 0; i < eachDimColumnValueSize.length; i++) { if (!isDictionaryColumn.get(i)) { eachDimColumnValueSize[i] = -1; continue; } eachDimColumnValueSize[i] = dictionayDimColumnValueSize[++index]; } if (complexDimensions.size() > 0) { int[] complexDimesionParition = new int[complexDimColumnCardinality.length]; // as complex dimension will be stored in column format add one Arrays.fill(complexDimesionParition, 1); bitLength = CarbonUtil.getDimensionBitLength(complexDimColumnCardinality, complexDimesionParition); for (int i = 0; i < bitLength.length; i++) { if (complexDimColumnCardinality[i] == 0) { bitLength[i] = 64; } } ColumnarSplitter keySplitter = new MultiDimKeyVarLengthVariableSplitGenerator(bitLength, complexDimesionParition); eachComplexDimColumnValueSize = keySplitter.getBlockKeySize(); } else { eachComplexDimColumnValueSize = new int[0]; } }