Questions tagged [defensive-copy]

31 questions
18
votes
7 answers

Defensive copy of Calendar

Been trying to find the best way to implement a method that makes a defensive copy of a Calendar object. eg: public void setDate(Calendar date) { // What to do.... } I am particularly concerned about interleaving of threads when checking for…
JJ Vester
  • 219
  • 1
  • 2
  • 9
5
votes
2 answers

Inefficiency of defensive copy in Java

I'm a longtime C/C++ programmer who's learning Java. I've read about the problem of breaking encapsulation by having an accessor method that returns a reference to a private field. The standard Java solution seems to be defensive copying - calling…
4
votes
1 answer

If I have a c# readonly struct that has non readonly structs as members, will compiler create defensive copy with in parameter

so, if I have for example a struct PlayerData that has members of Vector3 structs defined in System.Numerics (not readonly structs) public readonly struct PlayerData { public readonly Vector3 SpawnPoint; public readonly Vector3…
JohnE
  • 41
  • 2
4
votes
2 answers

Objective-C defensive copying in accessor methods

Coming from a Java background, I'm having trouble figuring out ways to program defensively in Objective-C. Assuming SomeClass is mutable and provides a copy method, this is a typical piece of code I'd write in Java: public MyClass { private…
Dmitry
  • 161
  • 1
  • 1
  • 10
3
votes
4 answers

Encapsulating a mutable object into a read-only object

I'm currently implementing iterative solvers, which work by successively improving the estimate the solution to a specific problem. As the solution is a rather large set of data, refinement is carried out in place. I have implemented a simple…
Sebastien
  • 246
  • 1
  • 12
3
votes
2 answers

How do I make defensive copy of an object?

How do I make defensive copies of a Mutable Object which contains a mutable field in an Immutable Object? class ImmutableObject { private final MutableObject immutable_field; ImmutableObject(MutableObject y) { this.immutable_field = y; …
unj2
  • 52,135
  • 87
  • 247
  • 375
3
votes
3 answers

How to deal with various cases of defensive programming?

This is an example of defensive copy in Effective java. Assume that scenario's in my underlying questions need a defensive copy, and cannot do with a comment asking client to avoid mutating objects passed in. public Period(Date start, Date end) { …
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
3
votes
2 answers

Regarding an Immutable class defensive copying

I have a query regarding creating a Immutable class. Following are the points which I take in consideration: Make the class final Make all members final, set them explicitly, in a static block, or in the constructor Make all members private No…
Crazy4Java
  • 269
  • 2
  • 6
  • 13
2
votes
1 answer

Create a Defensive Copy in the Constructor

The following is just example code to explain the problem I have trouble understanding: Lets say I have the following Professor class, note the public getters and setters: public class Professor { public string id {get; set; } …
user5910494
2
votes
3 answers

Making a general copy of a Set in constructor

I did not find a direct answer to this specific question, so... Assume class: class MyClass { private final Set tags; MyClass(Set initialTags) { this.tags = ???(initialtags); } } I just want a copy without…
hyde
  • 60,639
  • 21
  • 115
  • 176
1
vote
5 answers

How do I append/prepend list in function without modifying source?

Given this source list: source_list = [2, 3, 4] and this function: def function(list_in): list_in.append(5) list_in.insert(0, 1) return list_in As expected, I get: >>> function(source_list) [1, 2, 3, 4, 5] But if I call the variable…
1
vote
1 answer

SETQ or SETF With Defensive Copy

I am wondering about how one could do something as follows in Common Lisp. Suppose I have an object (entity) in memory that is unique at a certain time. What I would like to do is set some variable to be the state of that object, as snapshot at a…
MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
1
vote
1 answer

Correct Way of Creating a Defensive Copy of Custom Class

I've got the following class: public class FolderAgent { public string directoryName {get; private set; } public int numberofdirectories {get; private set; } public int numberofFiles {get; private set;} public…
user5910494
1
vote
2 answers

How do we make a java class immutable if it has a member variable of another user defined class?

Recently in an interview for Java Developer role, I was asked how do I make Class A immutable if it has a member variable, which is an object of Class B and in a situation where Class B is external to the project and cannot be edited by the…
1
vote
5 answers

LinkedList insert tied to inserted object

I have code that looks like this: public class Polynomial { List term = new LinkedList(); and it seems that whenever I do something like term.add(anotherTerm), with anotherTerm being... another Term object, it seems anotherTerm is…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
1
2 3