[x3d-public] Better approachs, was: Re: Java SOCKS! jsail/Navigation/OrthoViewpoint.java in X3DJSAIL
John Carlson
yottzumm at gmail.com
Tue Jul 29 23:35:49 PDT 2025
Corrections, learn something new daily!
If you google “immutable arrays in Java” the ai overview is good. Make a
copy when returning, make final variables private. That’s the easy
solution.
Also, the AI says:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/* [ editor’s note ] I doubt this is feasible, unless we hide stuff in a
private constructor and call the private constructor with this(…) at the
beginning ordinary constructors. Maybe worth a try, but likely the
stylesheet would be rough. It’s probably easier to return a copy. But
perhaps you could do the below when initializing a final List? Worth a
try? I’m not familiar with most of the stylesheet I’m not sure if List
can be passed to equals()???*/
public class MyClass {
private final List<Integer> immutableList;
public MyClass(int[] initialArray) {
this.immutableList =
Collections.unmodifiableList(Arrays.asList(Arrays.stream(initialArray).boxed().toArray(Integer[]::new)));
}
public List<Integer> getImmutableList() {
return immutableList;
}
}
Reference to documentation:
https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-
There are other unmodifiable collections.
I’ll probably mess with my OArray.java to try this out, interesting!
On Tue, Jul 29, 2025 at 11:55 PM John Carlson <yottzumm at gmail.com> wrote:
> Apparently, the FIELDOFVIEW_DEFAULT_VALUE should be final (immutable). I
> think the story is, you can modify elements of an ArrayList<Float>, but you
> can't modify the variable.
>
> The magenta? Looks wrong. A final variable got overwritten.
> ##########################################
> ((((getFieldOfView().length > 0) &&
> !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)) ||
> !ConfigurationProperties.getStripDefaultAttributes()) /* ArrayList .x3d
> compare */ && !hasUSE())=false
> getFieldOfView().length > 0=true
> !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)=false
> !ConfigurationProperties.getStripDefaultAttributes()=false
> !hasUSE()=true
> getFieldOfViewList()=[0.0, 0.0, 20.0, 20.0]
> FIELDOFVIEW_DEFAULT_VALUE=[0.0, 0.0, 20.0, 20.0]
> ##########################################
>
> Later, the magenta is correct. Why?
>
> ##########################################
> ((((getFieldOfView().length > 0) &&
> !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)) ||
> !ConfigurationProperties.getStripDefaultAttributes()) /* ArrayList .x3d
> compare */ && !hasUSE())=false
> getFieldOfView().length > 0=true
> !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)=false
> !ConfigurationProperties.getStripDefaultAttributes()=false
> !hasUSE()=true
> getFieldOfViewList()=[-1.0, -1.0, 1.0, 1.0]
> FIELDOFVIEW_DEFAULT_VALUE=[-1.0, -1.0, 1.0, 1.0]
> ##########################################
>
> Here's the definition of the variable:
>
> /** MFFloat field named <i>fieldOfView</i> has default value
> <i>{-1f,-1f,1f,1f}</i> (Java syntax) or <i>-1 -1 1 1</i> (XML syntax). */
> public static final ArrayList<Float> FIELDOFVIEW_DEFAULT_VALUE = new
> ArrayList<>(Arrays.asList(-1f,-1f,1f,1f));
>
> See example code to test (it gets even worse, even arrays themselves are
> mutable.
>
> import java.util.ArrayList;
> import java.util.Arrays;
>
> public class OArray {
> public static final ArrayList<Float> FIELDOFVIEW_DEFAULT_VALUE =
> new ArrayList<>(Arrays.asList(-1f,-1f,1f,1f));
> public static final float[] ARRAY_FIELDOFVIEW_DEFAULT_VALUE = new
> float [] {-1f,-1f,1f,1f};
> public static void main(String[] args) {
> System.out.println(FIELDOFVIEW_DEFAULT_VALUE);
> FIELDOFVIEW_DEFAULT_VALUE.set(0, 0f);
> FIELDOFVIEW_DEFAULT_VALUE.set(1, 0f);
> FIELDOFVIEW_DEFAULT_VALUE.set(2, 20f);
> FIELDOFVIEW_DEFAULT_VALUE.set(3, 20f);
> System.out.println(FIELDOFVIEW_DEFAULT_VALUE);
> System.out.print(
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0]+" "+
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1]+" "+
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2]+" "+
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3]);
> System.out.println("");
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0] = 0f;
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1] = 0f;
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2] = 20f;
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3] = 20f;
> System.out.print(
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0]+" "+
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1]+" "+
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2]+" "+
> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3]);
> System.out.println("");
> }
> }
> $ java net/coderextreme/OArray
> [-1.0, -1.0, 1.0, 1.0]
> [0.0, 0.0, 20.0, 20.0]
> -1.0 -1.0 1.0 1.0
> 0.0 0.0 20.0 20.0
>
> So, yeah, Java socks! The only thing that isn't final/mutable is
> variables.
>
> Don't you love the C foundation Java is based on?
>
> Anyone for Haskell?
>
> John
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20250730/6e4fd6b9/attachment-0001.html>
More information about the x3d-public
mailing list