Skip to content
Permalink
Browse files

8236980: Cleanup of toString methods in JavaSound

Reviewed-by: prr
  • Loading branch information
mrserb committed Apr 27, 2020
1 parent c18080f commit a0a9595d8bd4a8d9946caa6148fd5dff04b47adb
Showing with 397 additions and 185 deletions.
  1. +3 −3 src/java.desktop/share/classes/javax/sound/midi/MidiDevice.java
  2. +4 −5 src/java.desktop/share/classes/javax/sound/midi/Sequencer.java
  3. +14 −23 src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java
  4. +27 −64 src/java.desktop/share/classes/javax/sound/sampled/AudioFormat.java
  5. +5 −4 src/java.desktop/share/classes/javax/sound/sampled/BooleanControl.java
  6. +10 −15 src/java.desktop/share/classes/javax/sound/sampled/CompoundControl.java
  7. +5 −6 src/java.desktop/share/classes/javax/sound/sampled/Control.java
  8. +20 −17 src/java.desktop/share/classes/javax/sound/sampled/DataLine.java
  9. +5 −4 src/java.desktop/share/classes/javax/sound/sampled/EnumControl.java
  10. +6 −5 src/java.desktop/share/classes/javax/sound/sampled/FloatControl.java
  11. +3 −3 src/java.desktop/share/classes/javax/sound/sampled/Line.java
  12. +6 −15 src/java.desktop/share/classes/javax/sound/sampled/LineEvent.java
  13. +4 −4 src/java.desktop/share/classes/javax/sound/sampled/Mixer.java
  14. +5 −4 src/java.desktop/share/classes/javax/sound/sampled/Port.java
  15. +13 −13 src/java.desktop/share/classes/javax/sound/sampled/ReverbType.java
  16. +38 −0 test/jdk/javax/sound/midi/Devices/ToString.java
  17. +38 −0 test/jdk/javax/sound/midi/Sequencer/ToString.java
  18. +39 −0 test/jdk/javax/sound/sampled/AudioFileFormat/ToString.java
  19. +38 −0 test/jdk/javax/sound/sampled/AudioFormat/ToString.java
  20. +38 −0 test/jdk/javax/sound/sampled/Controls/ToString.java
  21. +38 −0 test/jdk/javax/sound/sampled/LineEvent/ToString.java
  22. +38 −0 test/jdk/javax/sound/sampled/ReverbType/ToString.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@@ -350,9 +350,9 @@ public final String getVersion() {
}

/**
* Provides a string representation of the device information.
* Returns a string representation of the info object.
*
* @return a description of the info object
* @return a string representation of the info object
*/
@Override
public final String toString() {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@@ -720,14 +720,13 @@ public final int hashCode() {
}

/**
* Provides this synchronization mode's name as the string
* representation of the mode.
* Returns mode's name as the string representation of the
* synchronization mode.
*
* @return the name of this synchronization mode
* @return a string representation of the synchronization mode
*/
@Override
public final String toString() {

return name;
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@@ -262,33 +262,25 @@ public Object getProperty(String key) {
}

/**
* Provides a string representation of the file format.
* Returns a string representation of the audio file format.
*
* @return the file format as a string
* @return a string representation of the audio file format
*/
@Override
public String toString() {

StringBuffer buf = new StringBuffer();

String str = "Unknown file format";
//$$fb2002-11-01: fix for 4672864: AudioFileFormat.toString() throws unexpected NullPointerException
if (type != null) {
buf.append(type.toString() + " (." + type.getExtension() + ") file");
} else {
buf.append("unknown file format");
if (getType() != null) {
str = getType() + " (." + getType().getExtension() + ") file";
}

if (byteLength != AudioSystem.NOT_SPECIFIED) {
buf.append(", byte length: " + byteLength);
if (getByteLength() != AudioSystem.NOT_SPECIFIED) {
str += ", byte length: " + getByteLength();
}

buf.append(", data format: " + format);

if (frameLength != AudioSystem.NOT_SPECIFIED) {
buf.append(", frame length: " + frameLength);
str += ", data format: " + getFormat();
if (getFrameLength() != AudioSystem.NOT_SPECIFIED) {
str += ", frame length: " + getFrameLength();
}

return new String(buf);
return str;
}

/**
@@ -376,10 +368,9 @@ public final int hashCode() {
}

/**
* Provides the file type's name as the {@code String} representation of
* the file type.
* Returns type's name as the string representation of the file type.
*
* @return the file type's name
* @return a string representation of the file type
*/
@Override
public final String toString() {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@@ -448,83 +448,47 @@ public boolean matches(AudioFormat format) {
}

/**
* Returns a string that describes the format, such as: "PCM SIGNED 22050 Hz
* 16 bit mono big-endian". The contents of the string may vary between
* implementations of Java Sound.
* Returns a string that describes the audio format, such as: "PCM SIGNED
* 22050 Hz 16 bit mono big-endian". The contents of the string may vary
* between implementations of Java Sound.
*
* @return a string that describes the format parameters
* @return a string representation of the audio format
*/
@Override
public String toString() {
String sEncoding = "";
if (getEncoding() != null) {
sEncoding = getEncoding().toString() + " ";
}

String sSampleRate;
if (getSampleRate() == (float) AudioSystem.NOT_SPECIFIED) {
sSampleRate = "unknown sample rate, ";
} else {
sSampleRate = "" + getSampleRate() + " Hz, ";
}
String sampleRate = getSampleRate() == AudioSystem.NOT_SPECIFIED ?
"unknown sample rate" : getSampleRate() + " Hz";

String sSampleSizeInBits;
if (getSampleSizeInBits() == (float) AudioSystem.NOT_SPECIFIED) {
sSampleSizeInBits = "unknown bits per sample, ";
} else {
sSampleSizeInBits = "" + getSampleSizeInBits() + " bit, ";
}
String sampleSize = getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED ?
"unknown bits per sample" : getSampleSizeInBits() + " bit";

String sChannels;
if (getChannels() == 1) {
sChannels = "mono, ";
} else
if (getChannels() == 2) {
sChannels = "stereo, ";
} else {
if (getChannels() == AudioSystem.NOT_SPECIFIED) {
sChannels = " unknown number of channels, ";
} else {
sChannels = ""+getChannels()+" channels, ";
}
}
String channels = switch (getChannels()) {
case 1 -> "mono";
case 2 -> "stereo";
case AudioSystem.NOT_SPECIFIED -> "unknown number of channels";
default -> getChannels() + " channels";
};

String sFrameSize;
if (getFrameSize() == (float) AudioSystem.NOT_SPECIFIED) {
sFrameSize = "unknown frame size, ";
} else {
sFrameSize = "" + getFrameSize()+ " bytes/frame, ";
}
String frameSize = getFrameSize() == AudioSystem.NOT_SPECIFIED ?
"unknown frame size" : getFrameSize() + " bytes/frame";

String sFrameRate = "";
String frameRate = "";
if (Math.abs(getSampleRate() - getFrameRate()) > 0.00001) {
if (getFrameRate() == (float) AudioSystem.NOT_SPECIFIED) {
sFrameRate = "unknown frame rate, ";
} else {
sFrameRate = getFrameRate() + " frames/second, ";
}
frameRate = getFrameRate() == AudioSystem.NOT_SPECIFIED ?
", unknown frame rate":", " + getFrameRate() + " frames/second";
}

String sEndian = "";
String bigEndian = "";
if ((getEncoding().equals(Encoding.PCM_SIGNED)
|| getEncoding().equals(Encoding.PCM_UNSIGNED))
&& ((getSampleSizeInBits() > 8)
|| (getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED))) {
if (isBigEndian()) {
sEndian = "big-endian";
} else {
sEndian = "little-endian";
}
bigEndian = isBigEndian() ? ", big-endian" : ", little-endian";
}

return sEncoding
+ sSampleRate
+ sSampleSizeInBits
+ sChannels
+ sFrameSize
+ sFrameRate
+ sEndian;

return String.format("%s %s, %s, %s, %s%s%s", getEncoding(),
sampleRate, sampleSize, channels, frameSize,
frameRate, bigEndian);
}

/**
@@ -630,13 +594,12 @@ public final int hashCode() {
}

/**
* Provides the {@code String} representation of the encoding. This
* {@code String} is the same name that was passed to the constructor.
* Returns encoding's name as the string representation of the encoding.
* For the predefined encodings, the name is similar to the encoding's
* variable (field) name. For example, {@code PCM_SIGNED.toString()}
* returns the name "PCM_SIGNED".
*
* @return the encoding name
* @return a string representation of the encoding
*/
@Override
public final String toString() {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@@ -119,13 +119,14 @@ public String getStateLabel(boolean state) {
}

/**
* Provides a string representation of the control.
* Returns a string representation of the boolean control.
*
* @return a string representation of the control
* @return a string representation of the boolean control
*/
@Override
public String toString() {
return new String(super.toString() + " with current value: " + getStateLabel(getValue()));
return String.format("%s with current value: %s", super.toString(),
getStateLabel(getValue()));
}

/**
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@@ -25,6 +25,8 @@

package javax.sound.sampled;

import java.util.StringJoiner;

/**
* A {@code CompoundControl}, such as a graphic equalizer, provides control over
* two or more related properties, each of which is itself represented as a
@@ -61,25 +63,18 @@ protected CompoundControl(Type type, Control[] memberControls) {
}

/**
* Provides a string representation of the control.
* Returns a string representation of the compound control.
*
* @return a string description
* @return a string representation of the compound control
*/
@Override
public String toString() {

StringBuilder sb = new StringBuilder();
for (int i = 0; i < controls.length; i++) {
if (i != 0) {
sb.append(", ");
if ((i + 1) == controls.length) {
sb.append("and ");
}
}
sb.append(controls[i].getType());
StringJoiner controls = new StringJoiner(", ", "[", "]");
for (Control control : getMemberControls()) {
controls.add(control.getType().toString());
}

return new String(getType() + " Control containing " + sb + " Controls.");
return String.format("%s containing %s controls", super.toString(),
controls);
}

/**
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@@ -66,13 +66,13 @@ public Type getType() {
}

/**
* Obtains a string describing the control type and its current state.
* Returns a string representation of the control.
*
* @return a string representation of the control
*/
@Override
public String toString() {
return new String(getType() + " Control");
return String.format("%s control", getType());
}

/**
@@ -120,10 +120,9 @@ public final int hashCode() {
}

/**
* Provides the {@code String} representation of the control type. This
* {@code String} is the same name that was passed to the constructor.
* Returns type's name as the string representation of the control type.
*
* @return the control type name
* @return a string representation of the control type
*/
@Override
public final String toString() {

0 comments on commit a0a9595

Please sign in to comment.