Search in sources :

Example 71 with Function

use of com.google.common.base.Function in project android by JetBrains.

the class DeviceArtPainterTest method testCropData.

// This test no longer applies; it was used to convert assets with a lot of padding into more tightly cropped screenshots.
// We're preserving the code since for future device releases we might get new artwork which includes padding.
@Test
@Ignore
public void testCropData() throws Exception {
    // Apply crop
    DeviceArtPainter framePainter = DeviceArtPainter.getInstance();
    Device device = newDevice();
    File srcDir = DeviceArtDescriptor.getBundledDescriptorsFolder();
    File destDir = new File(myTemporaryFolder.newFolder(), "device-art");
    if (!destDir.exists()) {
        boolean ok = destDir.mkdirs();
        assertTrue(ok);
    }
    StringBuilder sb = new StringBuilder(1000);
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<!-- Copyright (C) 2013 The Android Open Source Project\n" + "\n" + "     Licensed under the Apache License, Version 2.0 (the \"License\");\n" + "     you may not use this file except in compliance with the License.\n" + "     You may obtain a copy of the License at\n" + "\n" + "          http://www.apache.org/licenses/LICENSE-2.0\n" + "\n" + "     Unless required by applicable law or agreed to in writing, software\n" + "     distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "     See the License for the specific language governing permissions and\n" + "     limitations under the License.\n" + "-->\n" + "<devices>\n" + "\n");
    for (DeviceArtDescriptor spec : framePainter.getDescriptors()) {
        sb.append("  <device id=\"");
        sb.append(spec.getId());
        sb.append("\" name=\"");
        sb.append(spec.getName());
        sb.append("\">\n");
        DeviceData deviceData = new DeviceData(device, spec);
        for (ScreenOrientation orientation : ScreenOrientation.values()) {
            if (orientation == ScreenOrientation.SQUARE) {
                continue;
            }
            if (orientation != ScreenOrientation.LANDSCAPE && spec.getId().startsWith("tv_")) {
                // Android TV only uses landscape orientation
                continue;
            }
            Rectangle cropRect = spec.getCrop(orientation);
            sb.append("    <orientation name=\"");
            sb.append(orientation.getResourceValue());
            sb.append("\" ");
            DeviceArtDescriptor descriptor = deviceData.getDescriptor();
            if (spec.getName().startsWith("Generic ") || cropRect == null || spec.getName().startsWith("Android TV")) {
                System.out.println("Nothing to do for " + spec.getId() + " orientation " + orientation);
                cropRect = new Rectangle(0, 0, descriptor.getFrameSize(orientation).width, descriptor.getFrameSize(orientation).height);
            }
            sb.append("size=\"");
            sb.append(Integer.toString(cropRect.width));
            sb.append(",");
            sb.append(Integer.toString(cropRect.height));
            sb.append("\" screenPos=\"");
            sb.append(Integer.toString(descriptor.getScreenPos(orientation).x - cropRect.x));
            sb.append(",");
            sb.append(Integer.toString(descriptor.getScreenPos(orientation).y - cropRect.y));
            sb.append("\" screenSize=\"");
            sb.append(Integer.toString(descriptor.getScreenSize(orientation).width));
            sb.append(",");
            sb.append(Integer.toString(descriptor.getScreenSize(orientation).height));
            sb.append("\"");
            if (descriptor.getDropShadow(orientation) != null) {
                sb.append(" shadow=\"");
                //noinspection ConstantConditions
                sb.append(descriptor.getDropShadow(orientation).getName());
                sb.append("\"");
            }
            if (descriptor.getFrame(orientation) != null) {
                sb.append(" back=\"");
                //noinspection ConstantConditions
                sb.append(descriptor.getFrame(orientation).getName());
                sb.append("\"");
            }
            if (descriptor.getReflectionOverlay(orientation) != null) {
                sb.append(" lights=\"");
                //noinspection ConstantConditions
                sb.append(descriptor.getReflectionOverlay(orientation).getName());
                sb.append("\"");
            }
            if (descriptor.getMask(orientation) != null) {
                sb.append(" mask=\"");
                //noinspection ConstantConditions
                sb.append(descriptor.getMask(orientation).getName());
                sb.append("\"");
            }
            sb.append("/>\n");
            // Must use computeImage rather than getImage here since we want to get the
            // full size images, not the already cropped images
            writeCropped(srcDir, destDir, spec, cropRect, descriptor.getFrame(orientation));
            writeCropped(srcDir, destDir, spec, cropRect, descriptor.getDropShadow(orientation));
            writeCropped(srcDir, destDir, spec, cropRect, descriptor.getReflectionOverlay(orientation));
            writeCropped(srcDir, destDir, spec, cropRect, descriptor.getMask(orientation));
        }
        // (3) Rewrite emulator skin file
        File layoutFile = new File(srcDir, spec.getId() + File.separator + SdkConstants.FN_SKIN_LAYOUT);
        if (layoutFile.exists() && !spec.getId().startsWith("tv_")) {
            // no crop data in tv (and lack of portrait fails below)
            String layout = Files.toString(layoutFile, Charsets.UTF_8);
            final Rectangle portraitCrop = spec.getCrop(ScreenOrientation.PORTRAIT);
            if (portraitCrop != null) {
                final Rectangle landscapeCrop = spec.getCrop(ScreenOrientation.LANDSCAPE);
                layout = replace(layout, new String[] { "layouts {", "portrait {", "width " }, new Function<Integer, Integer>() {

                    @Override
                    public Integer apply(@Nullable Integer input) {
                        return portraitCrop.width;
                    }
                });
                layout = replace(layout, new String[] { "layouts {", "portrait {", "height " }, new Function<Integer, Integer>() {

                    @Override
                    public Integer apply(@Nullable Integer input) {
                        return portraitCrop.height;
                    }
                });
                layout = replace(layout, new String[] { "layouts {", "portrait {", "part2 {", "x " }, new Function<Integer, Integer>() {

                    @Override
                    public Integer apply(@Nullable Integer input) {
                        //noinspection ConstantConditions
                        return input - portraitCrop.x;
                    }
                });
                layout = replace(layout, new String[] { "layouts {", "portrait {", "part2 {", "y " }, new Function<Integer, Integer>() {

                    @Override
                    public Integer apply(@Nullable Integer input) {
                        //noinspection ConstantConditions
                        return input - portraitCrop.y;
                    }
                });
                // landscape
                layout = replace(layout, new String[] { "layouts {", "landscape {", "width " }, new Function<Integer, Integer>() {

                    @Override
                    public Integer apply(@Nullable Integer input) {
                        return landscapeCrop.width;
                    }
                });
                layout = replace(layout, new String[] { "layouts {", "landscape {", "height " }, new Function<Integer, Integer>() {

                    @Override
                    public Integer apply(@Nullable Integer input) {
                        return landscapeCrop.height;
                    }
                });
                layout = replace(layout, new String[] { "layouts {", "landscape {", "part2 {", "x " }, new Function<Integer, Integer>() {

                    @Override
                    public Integer apply(@Nullable Integer input) {
                        //noinspection ConstantConditions
                        return input - landscapeCrop.x;
                    }
                });
                layout = replace(layout, new String[] { "layouts {", "landscape {", "part2 {", "y " }, new Function<Integer, Integer>() {

                    @Override
                    public Integer apply(@Nullable Integer input) {
                        //noinspection ConstantConditions
                        return input - landscapeCrop.y;
                    }
                });
                File outputLayoutFile = new File(destDir, spec.getId() + File.separator + SdkConstants.FN_SKIN_LAYOUT);
                if (!outputLayoutFile.getParentFile().exists()) {
                    boolean mkdirs = outputLayoutFile.getParentFile().mkdirs();
                    assertTrue(mkdirs);
                }
                Files.write(layout, outputLayoutFile, Charsets.UTF_8);
            } else {
            // No crop data found; this device frame has already been cropped
            }
        }
        sb.append("  </device>\n\n");
    }
    sb.append("\n</devices>\n");
    File deviceArt = new File(destDir, "device-art.xml");
    Files.write(sb.toString(), deviceArt, Charsets.UTF_8);
    System.out.println("Wrote device art file " + deviceArt);
}
Also used : Device(com.android.sdklib.devices.Device) ScreenOrientation(com.android.resources.ScreenOrientation) Function(com.google.common.base.Function) DeviceData(com.android.tools.idea.ddms.screenshot.DeviceArtPainter.DeviceData) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) Ignore(org.junit.Ignore) DeviceSchemaTest(com.android.dvlib.DeviceSchemaTest) Test(org.junit.Test)

Example 72 with Function

use of com.google.common.base.Function in project beam by apache.

the class WithMetricsSupport method aggregatorMetricToGauges.

private Function<Map.Entry<String, Metric>, Map<String, Gauge>> aggregatorMetricToGauges() {
    return new Function<Map.Entry<String, Metric>, Map<String, Gauge>>() {

        @Override
        public Map<String, Gauge> apply(final Map.Entry<String, Metric> entry) {
            final NamedAggregators agg = ((AggregatorMetric) entry.getValue()).getNamedAggregators();
            final String parentName = entry.getKey();
            final Map<String, Gauge> gaugeMap = Maps.transformEntries(agg.renderAll(), toGauge());
            final Map<String, Gauge> fullNameGaugeMap = Maps.newLinkedHashMap();
            for (Map.Entry<String, Gauge> gaugeEntry : gaugeMap.entrySet()) {
                fullNameGaugeMap.put(parentName + "." + gaugeEntry.getKey(), gaugeEntry.getValue());
            }
            return Maps.filterValues(fullNameGaugeMap, Predicates.notNull());
        }
    };
}
Also used : Function(com.google.common.base.Function) Metric(com.codahale.metrics.Metric) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SortedMap(java.util.SortedMap) NamedAggregators(org.apache.beam.runners.spark.aggregators.NamedAggregators) Gauge(com.codahale.metrics.Gauge)

Example 73 with Function

use of com.google.common.base.Function in project jackrabbit-oak by apache.

the class OakFileDataStore method getAllRecords.

@Override
public Iterator<DataRecord> getAllRecords() {
    final String path = normalizeNoEndSeparator(new File(getPath()).getAbsolutePath());
    final OakFileDataStore store = this;
    return Files.fileTreeTraverser().postOrderTraversal(new File(path)).filter(new Predicate<File>() {

        @Override
        public boolean apply(File input) {
            return input.isFile() && !input.getParent().equals(path);
        }
    }).transform(new Function<File, DataRecord>() {

        @Override
        public DataRecord apply(File input) {
            return new FileDataRecord(store, new DataIdentifier(input.getName()), input);
        }
    }).iterator();
}
Also used : Function(com.google.common.base.Function) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) FileDataRecord(org.apache.jackrabbit.core.data.FileDataRecord) File(java.io.File)

Example 74 with Function

use of com.google.common.base.Function in project jackrabbit-oak by apache.

the class OakFileDataStoreTest method testGetAllIdentifiers.

private void testGetAllIdentifiers(String path, String unnormalizedPath) throws Exception {
    File testDir = new File(path);
    FileUtils.touch(new File(testDir, "ab/cd/ef/abcdef"));
    FileUtils.touch(new File(testDir, "bc/de/fg/bcdefg"));
    FileUtils.touch(new File(testDir, "cd/ef/gh/cdefgh"));
    FileUtils.touch(new File(testDir, "c"));
    FileDataStore fds = new OakFileDataStore();
    fds.setPath(unnormalizedPath);
    fds.init(null);
    Iterator<DataIdentifier> dis = fds.getAllIdentifiers();
    Set<String> fileNames = Sets.newHashSet(Iterators.transform(dis, new Function<DataIdentifier, String>() {

        @Override
        public String apply(@Nullable DataIdentifier input) {
            return input.toString();
        }
    }));
    Set<String> expectedNames = Sets.newHashSet("abcdef", "bcdefg", "cdefgh");
    assertEquals(expectedNames, fileNames);
    FileUtils.cleanDirectory(testDir);
}
Also used : Function(com.google.common.base.Function) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) File(java.io.File) FileDataStore(org.apache.jackrabbit.core.data.FileDataStore) Nullable(javax.annotation.Nullable)

Example 75 with Function

use of com.google.common.base.Function in project jackrabbit-oak by apache.

the class SharedDataStoreUtilsTest method testGetAllRecords.

@Test
public void testGetAllRecords() throws Exception {
    File rootFolder = folder.newFolder();
    dataStore = getBlobStore(rootFolder);
    int number = 10;
    Set<String> added = newHashSet();
    for (int i = 0; i < number; i++) {
        String rec = dataStore.addRecord(randomStream(i, 16516)).getIdentifier().toString();
        added.add(rec);
    }
    Set<String> retrieved = newHashSet(Iterables.transform(newHashSet(dataStore.getAllRecords()), new Function<DataRecord, String>() {

        @Nullable
        @Override
        public String apply(@Nullable DataRecord input) {
            return input.getIdentifier().toString();
        }
    }));
    assertEquals(added, retrieved);
}
Also used : Function(com.google.common.base.Function) DataRecord(org.apache.jackrabbit.core.data.DataRecord) File(java.io.File) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Aggregations

Function (com.google.common.base.Function)616 List (java.util.List)138 ArrayList (java.util.ArrayList)114 Nullable (javax.annotation.Nullable)103 Map (java.util.Map)97 IOException (java.io.IOException)89 HashMap (java.util.HashMap)78 Test (org.junit.Test)73 ImmutableList (com.google.common.collect.ImmutableList)49 File (java.io.File)46 Set (java.util.Set)46 Collection (java.util.Collection)35 ImmutableMap (com.google.common.collect.ImmutableMap)34 DateTime (org.joda.time.DateTime)33 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)29 HashSet (java.util.HashSet)27 Iterator (java.util.Iterator)27 LinkedList (java.util.LinkedList)26 ExecutionException (java.util.concurrent.ExecutionException)24 Collectors (java.util.stream.Collectors)15