How do you preserve blank lines between the MSpec delegate types? - mspec

Prior to Resharper 9, the default code reformatting preserved up to 2 blank lines between fields which meant that you could keep a blank line between your Because and It delegates like so:
public class when_something
{
static object obj1;
static object obj2;
static object obj3;
Establish context = () => { };
Because of;
It should_1;
It should_2;
}
As of Resharper 9, the default is to remove blank lines around single line fields, so it collapses everything together like so:
public class when_something
{
static object obj1;
static object obj2;
static object obj3;
Establish context = () => { };
Because of;
It should_1;
It should_2;
}
The default is already set to Preserve Existing Formatting -> Keep max blank lines in code = 2, but the Blank Lines setting for single line field seems to override this. If you change the setting under Blank Lines then it forces you to have blank lines and you will end up with blank lines after all your fields like so:
public class when_something
{
static object obj1;
static object obj2;
static object obj3;
Establish context = () => { };
Because of;
It should_1;
It should_2;
}
How do you configure Resharper 9 to preserve the blank lines if they are there?

Related

Expandable Objects - Managed C++

I try to develop custom control in managed c++, My custom cuntrol inherits from the Control class, As part of it i want to add custom properties to my custom control, to do that i created a class called ControlStyle, this class contains some properties like: colors, font etc.
Now i want to make a property of type ControlStyle in the CustomControl class, I'll explain myself: I want a property of type ControlStyle that will shown with the "+" sign in the properties window (called expandable properties), in the class CustomControl. so i did it, the problem is: that i drag and drop the custom control to a form and modify the values of the ControlStyle properties, and when i execute the application i see that any value of the ControlStyle properties that were changed by the user at the design time - not saved, and waht i get at runtime is the initial value of the ControlStyle properties, I understand that i'm not so clear, so lets talk code:
My ControlStyle class:
[TypeConverter(typeid(ExpandableObjectConverter))]
public ref class ControlStyle
{
// private variables
private: System::Drawing::Font^ font;
private: Color backColor1 = Color::Blue;
private: Color backColor2 = Color::Red;
// constructor
public: ControlStyle::ControlStyle()
{
font = gcnew System::Drawing::Font("Arial", 12);
}
// properties
public: property Color BackColor1
{
Color get() { retun backColor1; }
void set(Color value) { backColor1 = value; }
}
public: property Color BackColor2
{
Color get() { retun backColor2; }
void set(Color value) { backColor2 = value; }
}
public: virtual property System::Drawing::Font^ Font
{
virtual System::Drawing::Font^ get() override { return font; }
virtual void set(System::Drawing::Font^ value) override
{ font = value; }
}
}
My CustomControl class:
public ref class CustomControl : public System::Windows::Forms::Control
{
// private variables
private: ControlStyle^ controlStyle;
// constructor
public: CustomControl::CustomControl()
{
controlStyle = gcnew ControlStyle();
}
// properties
public: property ControlStyle^ Style
{
ControlStyle^ get() { return controlStyle; }
}
// methods like paint etc.
}
When i use the custom control on a form, (i see the first values of the ControlStyle properties like the initialization of them in the ControlStyle class), and i modify the values of the ControlStyle properties (Note: i modify the values of ControlStyle properties not by code, but manually by the properties window and i see the changes on the control - it works well), for example:
customControl1->Style->BackColor1 = Color::Green;
When i execute the program i see (at runtime) that the value of the customControl1->Style->BackColor1 property is Color::Blue that is the value of it's initial definition, and not Color::Green - which is the value that i put at design time.
To solve the issue i wrote the values that the customControl1->Style->BackColor1 property gets by the user at design time to a file and read it back when the DesignMode property change it's value to false, and additional attempts like this - and nothing...
I saw blogs says that it problem of serialization.
also applying the SerializableAttribute to the classes - does not work.
I think the reason for this problem is the initialization of the controlStyle variable in the CustomControl's constructor, i.e:
controlStyle = gcnew ControlStyle();
But this initialization must to be done for the control's function.
I will be very grateful for any help. Thanks alot!!

Accesing arraylist property from another class using constructor

So i have a class that makes an array list for me and i need to access it in another class through a constructor but i don't know what to put into the constructor because all my methods in that class are just for manipulating that list. im either getting a null pointer exception or a out of bounds exception. ive tried just leaving the constructor empty but that dosent seem to help. thanks in advance. i would show you code but my professor is very strict on academic dishonesty so i cant sorry if that makes it hard.
You are confusing the main question, with a potential solution.
Main Question:
I have a class ArrayListOwnerClass with an enclosed arraylist property or field.
How should another class ArrayListFriendClass access that property.
Potential Solution:
Should I pass the arraylist from ArrayListOwnerClass to ArrayListFriendClass,
in the ArrayListFriendClass constructor ?
It depends on what the second class does with the arraylist.
Instead of passing the list thru the constructor, you may add functions to read or change, as public, the elements of the hidden internal arraylist.
Note: You did not specify a programming language. I'll use C#, altought Java, C++, or similar O.O.P. could be used, instead.
public class ArrayListOwnerClass
{
protected int F_Length;
protected ArrayList F_List;
public ArrayListOwnerClass(int ALength)
{
this.F_Length = ALength;
this.F_List = new ArrayList(ALength);
// ...
} // ArrayListOwnerClass(...)
public int Length()
{
return this.F_Length;
} // int Length(...)
public object getAt(int AIndex)
{
return this.F_List[AIndex];
} // object getAt(...)
public void setAt(int AIndex, object AValue)
{
this.F_List[AIndex] = AValue;
} // void setAt(...)
public void DoOtherStuff()
{
// ...
} // void DoOtherStuff(...)
// ...
} // class ArrayListOwnerClass
public class ArrayListFriendClass
{
public void UseArrayList(ArrayListOwnerClass AListOwner)
{
bool CanContinue =
(AListOwner != null) && (AListOwner.Length() > 0);
if (CanContinue)
{
int AItem = AListOwner.getAt(5);
DoSomethingWith(Item);
} // if (CanContinue)
} // void UseArrayList(...)
public void AlsoDoesOtherStuff()
{
// ...
} // void AlsoDoesOtherStuff(...)
// ...
} // class ArrayListFriendClass
Note, that I could use an indexed property.

How to force MOXy to use the setter on a Collection property that is lazily initialized?

Given a bean like this:
public class MyBean {
private List<Something> things;
private List<Something> internalGetThings() {
if (things == null) {
things = new ArrayList<Something>();
}
return things;
}
public Iterable<Something> getThings() {
return <an immutable copy of internalGetThings()>;
}
public void setThings(List<Something> someThings) {
things.clear();
for (Something aThing : someThings) {
addThing(aThing);
}
}
public void addThing(Something aThing) {
things.add(aThing);
// Do some special stuff to aThing
}
}
Using external mapping file, when I map like this:
<xml-element java-attribute="things" name="thing" type="com.myco.Something" container-type="java.util.ArrayList" />
It seems that each individual Something is being added to the MyBean by calling getThings().add(). That's a problem because getThings() returns an immutable copy of the list, which is lazily initialized. How can I configure mapping (I'm using an external mapping file, not annotations) so that MOXy uses setThings() or addThing() instead?
Why Does JAXB/MOXy Check the Get Method for Collection First?
JAXB (JSR-222) implementations give you a chance to have your property be the List interface and still leverage the underlying List implementation that you choose to use. To accomplish this a JAXB implementation will call the get method to see if the List implementation has been initialized. It it has the List will be populated using the add method.
public List<String> getThings() {
if(null == things) {
things = new ArrayList<String>();
}
return things;
}
public List<String> getThings() {
if(null == things) {
things = new LinkedList<String>();
}
return things;
}
If you don't pre-initialize the List property then MOXy/JAXB will build an instance of the List (default is ArrayList) and set it on the object using the set method.
private List<Something> things; // Don't Initialize
public List<String> getThings() {
return things;
}
public void setThings(List<String> things) {
this.things = things;
}
Given the reason in #Blaise's answer, it doesn't seem possible to have MOXy (or any JAXB implementation in general?) populate a lazily-initialized collection via a setter method on the collection. However, a combination of xml-accessor-type="FIELD" (or #XmlAccessorType if using annotations) and defining a JAXB unmarshal event callback will get the job done. In my afterUnmarshal() implementation I do the special work on Something instances that is done in addSomething().
private void afterUnmarshal(Unmarshaller, Object parent) {
for (Something aThing : getSomethings()) {
// Do special stuff on aThing
}
}
Using FIELD access type gets JAXB/MOXy to directly inject the collection into the field, bypassing the getter. Then the call back cleans things up properly.

Nunit Assertion for an empty intersection between collection

I've looked all around, and can't quite figure this one out, and my multitude of trial and error attempts have all been useless.
I have a list of user names (we'll call 'original list') one object is returning
I have a list of user names (we'll call 'filtration list') another object is returning
I am testing a method that returns all of the items from the original list not in the filtration list.
Ideally what I want is something like
Assert.That(returnedList, Has.No.Members.In(filtrationList))
So far the only thing I can do is iterate over the filtrationList and do
Assert.That(returnedList, Has.None.EqualTo(filteredUser))
With nunit you can create any custom constraint.
If you want to verify two collections for intersection, you can create something like this:
public class Intersects : CollectionConstraint
{
private IEnumerable _collection2;
public Intersects(IEnumerable collection2)
: base(collection2)
{
_collection2 = collection2;
}
public static Intersects With(IEnumerable arg)
{
return new Intersects(arg);
}
protected override bool doMatch(IEnumerable collection)
{
foreach (object value in collection)
{
foreach (object value2 in _collection2)
if (value.Equals(value2))
return true;
}
return false;
}
public override void WriteDescriptionTo(MessageWriter writer)
{
//You can put here something more meaningful like items which should not be in verified collection.
writer.Write("intersecting collections");
}
}
usage is pretty simple:
string[] returnedList = new string[] { "Martin", "Kent", "Jack"};
List<string> filteredUsers = new List<string>();
filteredUsers.Add("Jack");
filteredUsers.Add("Bob");
Assert.That(returnedList, Intersects.With(filteredUsers));

How does Undo work?

How does undo work? Does it copy all the managed objects every time any of the values change? Or does it only copy the actual changes together with an information which objects were affected? Is that heavy or lightweight?
The 'undo' mechanism for pretty much any language that supports Object-Oriented constructs uses the Memento Design Pattern to make it happen.
Here's a rough implementation to get you thinking. This handles your stack of undoable operations. (It doesn't handle redo, but that's easy to support by replacing the stack with a list and keeping track of the current item.)
public class Undoable {
public static void Do(Action do, Action undo) {
do();
sUndoStack.Push(new Undoable(do, undo));
}
public static void Undo() {
sUndoStack.Pop().mUndoCallback();
}
private Undoable(Action doCallback, undoCallback) {
mDoCallback = doCallback;
mUndoCallback = undoCallback;
}
private Action mDoCallback, mUndoCallback;
// note: using a global stack here is lame, but works for demo purposes
private static readonly Stack<Undoable> sUndoStack = new Stack<Undoable>();
}
To use this, let's say the user can change a value in some object like this:
public class Foo {
public string Bar {
get { return mBar; }
set {
if (mBar != value) {
mBar = value;
}
}
}
private string mBar;
}
To make that operation undoable, we just change the setter to:
set {
if (mBar != value) {
string oldValue = mBar;
Undoable.Do(() => mBar = value,
() => mBar = oldValue);
}
}
Now, if you call Undoable.Undo() from anywhere in the application, your instance of Foo will restore the previous value of Bar. If Foo also raises an event when Bar changes (not shown here), the UI will also properly refresh on undo too.