Sunday, April 29, 2012

Padding Wrong for Ordered Lists

As I'm making some modifications to my blog, I couldn't help but noticed that ordered lists.
  1. example
  2. example
  3. example
Are messed up with most templates I picked. So I wrote some simple css to fix that issue, under the advanced section of the template manager. I put this in the "Add CSS" text box.

ol {
    padding-left: 40px;
    padding-top: 10px;
}

Just throwing this out there in case someone else was just as confused as me.

Wednesday, April 25, 2012

Serialization Helper Methods

I keep forgetting the classes/methods I need to Serialize/Deserialize objects to and from XML. So I decided to make a couple of helper methods.


/// <summary>
/// Writes the serialized form of the object, in xml format, to the file
/// location specified.
/// </summary>
/// <typeparam name="T">type to serialize from</typeparam>
/// <param name="toSerialize">data to serialize</param>
/// <param name="fileLocation">to serialize the data too</param>
public static void Serialize<T>(T toSerialize, string fileLocation)
{
    if (toSerialize == null)
        throw new ArgumentException("The data to serialize cannot be null.");

    if (string.IsNullOrEmpty(fileLocation))
        throw new ArgumentException("You must provide a file location to output the data to.");

    using (var textWriter = new StreamWriter(fileLocation))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", ""); //turn off namespaces
        serializer.Serialize(textWriter, toSerialize, ns);
    }
}

/// <summary>
/// Reads the data from the file location specified and creates an instance
/// of the object type. The object must be serializable.
/// </summary>
/// <typeparam name="T">type to deserialize the data too</typeparam>
/// <param name="fileLocation">data to deserialize from</param>
/// <returns>new object of type T with properties filled out</returns>
public static T Deserialize<T>(string fileLocation)
{
    if (string.IsNullOrEmpty(fileLocation))
        throw new ArgumentException("You must provide a file location to read the data from.");

    using (var reader = XmlReader.Create(fileLocation))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(reader);
    }
}

They're pretty straight forward to use, but just for clarification.


public static void Main(string[] args)
{
    MyObject obj = Program.Deserialize<MyObject>(@"Files\SerializedData.xml");
    obj.AdjustValues();
    Console.WriteLine("Done!!!: {0} test: {1}", obj.Property1, obj.Property2);
    obj.AuditDirectoryName = "Test";
    Program.Serialize(obj, @"Files\SerializedData.xml");
    Console.ReadLine();
}

Short and Sweet

So today at work I had to come up with a method that would give me a listing of something from a collection. If the listing didn't exist, I wanted the listing to be initialized and automatically added to the list.

Let's look at my pathetic first attempt.


private Listing GetListing(List<Listing> cars, string name)
{
    Listing listing = null;
    IEnumerable<Listing> results = cars.Where(i => i.Name == name);

    //if map doesn't exist create it
    if (results.Count() == 0)
    {
        listing = new Listing()
        {
            Name = name,
            AdditionalData = new List<Data>()
        };

        cars.Add(listing);
     }
     else
     {
         //If we originally did this, it would throw an 
         //exception when there are no matches found. 
         //That's why this part is in the else block.
         listing = results.First();
     }

     return listing;
}

As you can see, I tried to get fancy and use Linq. I found, through testing, that the method .First() would throw an exception if there were no results. There were other methods, but for some reason I just couldn't think of the right method.

So then I threw my hands in the air and figured it'd be shorter to just write a method without Linq and that I was just over complicating it.

Thursday, September 23, 2010

Input

Here is a helper class that I created for getting input from the console in java. Created a java class named InputHelper and paste the content below and press ctrl+shift+f to format it.

Friday, August 13, 2010

Creating and Generating Java Doc

To generate java docs in code simply type "/**" and press enter. The first sentence you write is the summary and the rest in put in the details section.
Here is an example: (Unfortunately I couldn't format the code on here, sorry)
/**
* Get's the input from the user as a short. The input must be between the lower and
* upper bounds specified inclusive. If not between the values the user will be prompted
* with the error "Please input a number between (lower) and (upper) inclusive." The user
* will continue to be prompted with the message to display until valid input is given.
*
* @param lower bound
* @param upper bound
* @param display what to prompt the user before accepting input
* @return input from the user as a short
*/
public static short getShort(short lower, short upper, String display){
boolean valid = false; short result = 0;

while(!valid){ System.out.print(display);
String input = getString();
if(isWholeNumber(input)){
result = Short.parseShort(input);
valid = withinBounds(lower, upper, result);
}
}

return result;
}
To generate the html for the javadoc, right click on the project and select export. Type javadoc in the window as shown below and select javadoc.

Once your done specify the location of the javadoc tool. For me the path is "C:/Program Files/Java/jdk1.6.0_20/bin/javadoc.exe " As shown below.


Press finish and voila, your done generating javadocs.