changeset 2862:c3c28ce45273

4937708: AudioFormat.matches should allow NOT_SPECIFY in all fields Reviewed-by: denis
author amenkov
date Tue, 14 Sep 2010 14:07:58 +0400
parents 93d13ea00faf
children 167a6a4634f5
files src/share/classes/javax/sound/sampled/AudioFormat.java test/javax/sound/sampled/AudioFormat/Matches_NOT_SPECIFIED.java
diffstat 2 files changed, 127 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/sound/sampled/AudioFormat.java	Thu Sep 02 11:13:42 2010 -0700
+++ b/src/share/classes/javax/sound/sampled/AudioFormat.java	Tue Sep 14 14:07:58 2010 +0400
@@ -431,34 +431,36 @@
 
 
     /**
-     * Indicates whether this format matches the one specified.  To match,
-     * two formats must have the same encoding, the same number of channels,
-     * and the same number of bits per sample and bytes per frame.
-     * The two formats must also have the same sample rate,
-     * unless the specified format has the sample rate value <code>AudioSystem.NOT_SPECIFIED</code>,
-     * which any sample rate will match.  The frame rates must
-     * similarly be equal, unless the specified format has the frame rate
-     * value <code>AudioSystem.NOT_SPECIFIED</code>.  The byte order (big-endian or little-endian)
-     * must match if the sample size is greater than one byte.
+     * Indicates whether this format matches the one specified.
+     * To match, two formats must have the same encoding,
+     * and consistent values of the number of channels, sample rate, sample size,
+     * frame rate, and frame size.
+     * The values of the property are consistent if they are equal
+     * or the specified format has the property value
+     * {@code AudioSystem.NOT_SPECIFIED}.
+     * The byte order (big-endian or little-endian) must be the same
+     * if the sample size is greater than one byte.
      *
      * @param format format to test for match
-     * @return <code>true</code> if this format matches the one specified,
-     * <code>false</code> otherwise.
-     */
-    /*
-     * $$kk: 04.20.99: i changed the semantics of this.
+     * @return {@code true} if this format matches the one specified,
+     *         {@code false} otherwise.
      */
     public boolean matches(AudioFormat format) {
-
-        if (format.getEncoding().equals(getEncoding()) &&
-            ( (format.getSampleRate() == (float)AudioSystem.NOT_SPECIFIED) || (format.getSampleRate() == getSampleRate()) ) &&
-            (format.getSampleSizeInBits() == getSampleSizeInBits()) &&
-            (format.getChannels() == getChannels() &&
-             (format.getFrameSize() == getFrameSize()) &&
-             ( (format.getFrameRate() == (float)AudioSystem.NOT_SPECIFIED) || (format.getFrameRate() == getFrameRate()) ) &&
-             ( (format.getSampleSizeInBits() <= 8)  || (format.isBigEndian() == isBigEndian()) ) ) )
+        if (format.getEncoding().equals(getEncoding())
+                && (format.getChannels() == AudioSystem.NOT_SPECIFIED
+                    || format.getChannels() == getChannels())
+                && (format.getSampleRate() == (float)AudioSystem.NOT_SPECIFIED
+                    || format.getSampleRate() == getSampleRate())
+                && (format.getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED
+                    || format.getSampleSizeInBits() == getSampleSizeInBits())
+                && (format.getFrameRate() == (float)AudioSystem.NOT_SPECIFIED
+                    || format.getFrameRate() == getFrameRate())
+                && (format.getFrameSize() == AudioSystem.NOT_SPECIFIED
+                    || format.getFrameSize() == getFrameSize())
+                && (getSampleSizeInBits() <= 8
+                    || format.isBigEndian() == isBigEndian())) {
             return true;
-
+        }
         return false;
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/sound/sampled/AudioFormat/Matches_NOT_SPECIFIED.java	Tue Sep 14 14:07:58 2010 +0400
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 4937708
+ * @summary Tests that AudioFormat.matches handle NOT_SPECIFIED value in all fields
+ * @run main Matches_NOT_SPECIFIED
+ * @author Alex Menkov
+ *
+ */
+
+import javax.sound.sampled.AudioFormat;
+import javax.sound.sampled.AudioFormat.Encoding;
+import javax.sound.sampled.AudioSystem;
+
+
+public class Matches_NOT_SPECIFIED {
+
+    static boolean success = true;
+    static AudioFormat f1;
+    static AudioFormat f2;
+
+    public static void main(String[] args) throws Exception {
+        AudioFormat f3;
+        f1 = new AudioFormat(44100, 16, 2, true, false);
+        f2 = new AudioFormat(Encoding.PCM_SIGNED,
+                AudioSystem.NOT_SPECIFIED,
+                AudioSystem.NOT_SPECIFIED,
+                AudioSystem.NOT_SPECIFIED,
+                AudioSystem.NOT_SPECIFIED,
+                AudioSystem.NOT_SPECIFIED, false);
+        test(true);
+
+//        f1 = new AudioFormat(44100, 8, 16, true, false);
+        f2 = new AudioFormat(Encoding.PCM_SIGNED,
+                AudioSystem.NOT_SPECIFIED,
+                AudioSystem.NOT_SPECIFIED,
+                AudioSystem.NOT_SPECIFIED,
+                AudioSystem.NOT_SPECIFIED,
+                AudioSystem.NOT_SPECIFIED, true);
+        test(false);
+
+        f1 = new AudioFormat(44100, 8, 8, true, false);
+        test(true);
+
+        if (success) {
+            out("The test PASSED.");
+        } else {
+            out("The test FAILED.");
+            throw new Exception("The test FAILED");
+        }
+    }
+
+    static void test(boolean shouldMatch) {
+        out("testing:");
+        out("  - " + f1.toString());
+        out("  - " + f2.toString());
+        if (f1.matches(f2)) {
+            if (shouldMatch) {
+                out("  (OK) MATCHES");
+            } else {
+                out("  (ERROR) MATCHES");
+                success = false;
+            }
+        } else {
+            if (shouldMatch) {
+                out("  (ERROR) DOESNT MATCH!");
+                success = false;
+            } else {
+                out("  (OK) DOESNT MATCH!");
+            }
+        }
+    }
+
+    static void out(String s) {
+        System.out.println(s);
+    }
+
+}