Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BAF218B51 for ; Fri, 12 Aug 2011 03:42:36 +0000 (UTC) Received: (qmail 51134 invoked by uid 500); 12 Aug 2011 03:42:35 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 51036 invoked by uid 500); 12 Aug 2011 03:42:23 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 51027 invoked by uid 99); 12 Aug 2011 03:42:12 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Aug 2011 03:42:12 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of sebastien.brisard@gmail.com designates 209.85.220.171 as permitted sender) Received: from [209.85.220.171] (HELO mail-vx0-f171.google.com) (209.85.220.171) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Aug 2011 03:42:04 +0000 Received: by vxh13 with SMTP id 13so2481963vxh.30 for ; Thu, 11 Aug 2011 20:41:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type :content-transfer-encoding; bh=WeHXj2+9B/J4cLuJCM11KphXHhaWX4dDPQgc0ppXgbA=; b=C3aRjNP1OcsIc+xYiPK2F3g7YUS+W/LTOCPPBBgOHKnAgswwT962zgd9zE5wnYUgCi /s5jHjrqZ5WsIkSQpulNj7OHS1ytY6yaU0yvqWJ9nCZQAqO2eZ8SImZ5+nIjGBkxbxQg 05Qz0BcKNwmI+JWsy59nVywFff4v796RY8Vf8= MIME-Version: 1.0 Received: by 10.52.24.162 with SMTP id v2mr422863vdf.153.1313120503525; Thu, 11 Aug 2011 20:41:43 -0700 (PDT) Sender: sebastien.brisard@gmail.com Received: by 10.220.192.137 with HTTP; Thu, 11 Aug 2011 20:41:43 -0700 (PDT) In-Reply-To: <4E44351F.5060000@gmail.com> References: <1313048561.2738.2.camel@knuffelchen> <1313052399.2877.6.camel@knuffelchen> <4E43A1BB.3050508@free.fr> <20110811112236.GB2590@dusk.harfang.homelinux.org> <4E44351F.5060000@gmail.com> Date: Fri, 12 Aug 2011 05:41:43 +0200 X-Google-Sender-Auth: DFCqsMkTLlnHcFxj33syGS1-wdg Message-ID: Subject: Re: [math] Read-only RealVector From: =?ISO-8859-1?Q?S=E9bastien_Brisard?= To: Commons Developers List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi, it's clear I think that there is no really fool-proof solution, but that's OK I think. The idea would be to avoid accidental modifications which could be catastrophic. But nothing could prevent an evil programmer to do its evil job. I like what was earlier pointed out: Javadoc should be very detailed on these issues. About having a really immutable vector, it's of course a desirable feature, but thinking about it, it would be nice to be able to combine both immutable vectors and mutable vectors. Question is: should the result be mutable or immutable ? Example immutale.add(mutable), and the converse mutable.add(immutable). Should the results be mutable? immutable? Following the approach in UmmodifiableCollections is probably what I'll do, except if you like the following (VEEEEERY simple) approach better. Instead of having a method RealVector getSolution() how about specifying RealVector getSolution(boolean deep) if deep is true, it returns a deep copy, if not it MIGHT (but the contract of the method does not make it compulsory) return a shallow copy. The Javadoc should clearly state that if deep is true, then the user should not modify the returned vector. Looking forward to hearing what you think, Sebastien 2011/8/11 Phil Steitz : > On 8/11/11 4:22 AM, Gilles Sadowski wrote: >> Hello S=E9bastien. >> >>>> Well, in fact I would very much like to have immutable vectors too. >>>> Immutability is really a way to simplify implementations. Surprisingly= it >>>> sometimes also decrease time and memory consumption, because defensive >>>> copies littering user code can be avoided. >>>> >>> Luc, I have a silly question. Why do you think immutable vectors would >>> prevent defensive copy ? Creating an immutable vector from an existing >>> mutable vector would require a defensive copy, wouldn't it? >>> Thats the reason why I'm talking about read-only vectors, and not >>> immutable vectors. >>> The solver would keep a reference to the underlying (mutable) vector, >>> so would be able to modify (indirectly) the read-only vector. >>> The observer would only have a reference to the read-only vector, so >>> would not be able to modify the underlying vector. >> I think that you were right to stress the difference between immutable a= nd >> read-only. >> If I'm not mistaken, in JDK parlance they call the latter "unmodifiable" >> (cf. the methods "unmodifiableCollection" and siblings in the standard c= lass >> "java.util.Collections"). >> >> The idea which you referred to at the beginning of this thread is exactl= y >> what they do: The wrapping class throws "UnsupportedOperationException" = from >> any "mutating" method. >> >> What Luc said is that when you pass an (already) immutable object, you d= o not >> have to make a copy; a reference is as safe. >> >> I'm not sure whether that would still be true with an unmodifiable >> reference. I.e. couldn't it be cast to its underlying modifiable referen= ce? >> However that would be on-purpose nasty code, not a mistake on the part o= f >> the checker code. >> >> So, a utility method like >> =A0 public static RealVector unmodifiableRealVector(RealVector v) >> in class "AbstractRealVector", would fit your need, IMO. > > +1 - looks to me like what is needed is an unmodifiable view, > exactly like what the UnmodifiableCollections provide. =A0 What is > returned in the methods analogous to the above in > Collections.unModifiableXxx are instances of private static inner > classes that decorate the actual parameter and delegate, throwing > UnsupportedOperationExceptions for mutators. =A0See either > [collections] or the Harmony code [1] for examples. > > Phil > > [1] http://s.apache.org/XJ4 >> >> >> Best, >> Gilles >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org >> For additional commands, e-mail: dev-help@commons.apache.org >> >> > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > For additional commands, e-mail: dev-help@commons.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org