Archive

Posts Tagged ‘Class’

Class diagrams for Hotspotizer Kinect-based application source code

Following are class diagrams for Hotspotizer’s C# code, as is currently at 4 Aug 2015 at its refactored version I maintain in my fork at GitHub (http://github.com/birbilis/hotspotizer). The respective diagram (.cd) files for Visual Studio are available in the code repository at the various subfolders:

GUI

Models

Converters

Helpers

Fix: Java ServletException: IncompatibleClassChangeError

was getting an error like the following at your JavaEE Servlet (Portlets are also Servlets under the hood btw):

javax.servlet.ServletException: javax.portlet.PortletException: java.lang.Throwable: java.lang.IncompatibleClassChangeError: : incorrect call to interface method

 

at first I thought the issue was with:

  <bean:message class=”someCSSstyleClass” key="someMsgKey"/>

 

and used instead:

  <span class="someCSSstyleClass"><bean:message key="someMsgKey"/></span>

 

in case the “class” attribute at bean:message tag was meaning a Java class instead of a CSS class

But, kept  on getting the same error…

Then I tried a “Clean and Build” instead of just “Build” action at NetBeans IDE and errors poped up. Had refactored an abstract class to an interface and seems NetBeans was only compiling parts of my code which can cause tricky to debug errors.

So whenever you see strange runtime errors, especially when refactoring some older project, make sure you use “Clean and Build” (sometimes called “Rebuild”) in your IDE to be safe.

.NET String extension methods to check for array of prefixes or suffixes

Seems StartsWith and EndsWith methods of String class in .NET are missing a version that accepts multiple (as an array) prefixes or suffixes respectively when testing the string. To achieve this I just added the following extension methods to StringExtensions class (of Utils.Extensions namespace) under Utils.Silverlight project at the ClipFlair source code.

public static bool StartsWith(
this string s,
string[] suffixes,
StringComparison comparisonType = StringComparison.CurrentCulture) { foreach (string suffix in suffixes) if (s.StartsWith(suffix, comparisonType)) return true; return false; } public static bool EndsWith(
this string s,
string[] suffixes,
StringComparison comparisonType = StringComparison.CurrentCulture) { foreach (string suffix in suffixes) if (s.EndsWith(suffix, comparisonType)) return true; return false; }

 

To use them, you add a reference to Utils.Silverlight project to your own one and then add a using clause for the namespace that hosts a static class with these extension methods (e.g. “using Utils.Extensions;”) and then you can use them on any String at the respective source file. Can even use them on literal strings, since most .NET compilers support Boxing of literals into respective types.

I’m using a default value for the comparisonType method argument to make it optional. I use StringComparison.CurrentCulture as the default value for it (performing a word case-sensitive and culture-sensitive comparison using the current culture), as Microsoft is doing at “String.StartsWith(String)” method. However, do note the following text from that method’s documentation:

Notes to Callers

As explained in Best Practices for Using Strings in the .NET Framework, we recommend that you avoid calling string comparison methods that substitute default values and instead call methods that require parameters to be explicitly specified. To determine whether a string begins with a particular substring by using the string comparison rules of the current culture, call the StartsWith(String, StringComparison) method overload with a value of StringComparison.CurrentCulture for its comparisonType parameter.

.NET String class extensions to replace prefix or suffix

Just added the following extension methods to StringExtensions class (of Utils.Extensions namespace) under Utils.Silverlight project at the ClipFlair source code.

public static string ReplacePrefix(
this string s,
string fromPrefix,
string toPrefix,
StringComparison comparisonType = StringComparison.CurrentCulture) { return (s.StartsWith(fromPrefix, comparisonType)) ?
toPrefix + s.Substring(fromPrefix.Length) : s; } public static string ReplacePrefix(
this string s,
string[] fromPrefix,
string toPrefix,
StringComparison comparisonType = StringComparison.CurrentCulture) { foreach (string prefix in fromPrefix) if (s.StartsWith(prefix, comparisonType)) return toPrefix + s.Substring(prefix.Length); return s; } public static string ReplaceSuffix(
this string s,
string fromSuffix,
string toSuffix,
StringComparison comparisonType = StringComparison.CurrentCulture) { return (s.EndsWith(fromSuffix, comparisonType)) ?
s.Substring(0, s.Length - fromSuffix.Length) + toSuffix : s; } public static string ReplaceSuffix(
this string s,
string[] fromSuffix,
string toSuffix,
StringComparison comparisonType = StringComparison.CurrentCulture) { foreach (string suffix in fromSuffix) if (s.EndsWith(suffix, comparisonType)) return s.Substring(0, s.Length - suffix.Length) + toSuffix; return s; }

 

To use them, you add a reference to Utils.Silverlight project to your own one and then add a using clause for the namespace that hosts a static class with these extension methods (e.g. “using Utils.Extensions;”) and then you can use them on any String at the respective source file. Can even use them on literal strings, since most .NET compilers support Boxing of literals into respective types.

e.g.

s = s.ReplacePrefix("https://&quot;, "http://&quot;, StringComparison.OrdinalIgnoreCase);

//– converts https:// prefix to http:// ignoring character case

or

s = s.ReplacePrefix(new String[]{"https://&quot;, "http://&quot;}, "", StringComparison.OrdinalIgnoreCase);

//– removes https:// or http:// prefix ignoring character case

 

Update:

I added a default value for the comparisonType method argument to make it optional. I use StringComparison.CurrentCulture as the default value for it (performing a word case-sensitive and culture-sensitive comparison using the current culture), as Microsoft is doing at “String.StartsWith(String)” method. However, do note the following text from that method’s documentation:

Notes to Callers

As explained in Best Practices for Using Strings in the .NET Framework, we recommend that you avoid calling string comparison methods that substitute default values and instead call methods that require parameters to be explicitly specified. To determine whether a string begins with a particular substring by using the string comparison rules of the current culture, call the StartsWith(String, StringComparison) method overload with a value of StringComparison.CurrentCulture for its comparisonType parameter.

Combinatorics library (Silverlight for Windows Phone)

I recently found a very nice article at CodeProject on Combinatorics algorithms implementation (specifically Permutations, Combinations and Variations with Repetition option) using C# Generics:

http://www.codeproject.com/KB/recipes/Combinatorics.aspx

It uses a lexicographic algorithm, so it allows one to ask for “next” permutation/combination/variation from the collection of all possible ones, in the spirit of C++ Standard Library’s (STL) “next_permutation” algorithm.

My Anagram app for Windows Phone 7 is using this code (packed it in a Silverlight library)

I packed the files in a Silverlight for Windows Phone class library (named Combinatorics) and modification I had to do was to:

  1. make the IMetaCollection interface public so that I could abstract the use of any combinatorics operation based on user selection
  2. add CombinatoricsOp class to help with using any combinatorics operation via one entry point
  3. As a bonus there is also a Combinatorics.cd there (class diagram)

You can download the library to use with your WP7 Silverlight apps (Portable [PCL] version also available now) from http://github.com/zoomicon/Combinatorics
Please give due credit to original author of Combinatorics code (and myself if you use my additions too).

The code for CombinatoricsOp class I added is below. Just call GetCombinatoricsOp to get a Permutations, Combinations or Variations class instance (all implement IMetaCollection interface) and then use the enumerator (with extra method to get count of items without counting them one-by-one) as described in the CodeProject article mentioned above.

//Version: 20111119
//Author: George Birbilis (birbilis@kagi.com)

using System;
using System.Collections.Generic;   namespace Facet.Combinatorics
{   public static class CombinatoricsOp {   public enum CombinatoricsOpType
    {
      Permutations = 0,
      PermutationsWithRepetition = 1,
      //---
      Combinations = 2,
      CombinationsWithRepetition = 3,
      //---
      Variations = 4,
      VariationsWithRepetition = 5
    }   public static IMetaCollection<string> GetCombinatoricsOp(
                  IList<string> values, CombinatoricsOpType opType, int take){
     switch (opType)
     {
       case CombinatoricsOpType.Permutations:
         return new Permutations<string>(values);

case CombinatoricsOpType.PermutationsWithRepetition: return new Permutations<string>(values,GenerateOption.WithRepetition); case CombinatoricsOpType.Combinations: return new Combinations<string>(values, take); case CombinatoricsOpType.CombinationsWithRepetition: return new Combinations<string>(
values, take, GenerateOption.WithRepetition); case CombinatoricsOpType.Variations: return new Variations<string>(values, take); case CombinatoricsOpType.VariationsWithRepetition: return new Variations<string>(
values, take, GenerateOption.WithRepetition); default: throw new NotImplementedException(); } }   }   }

%d bloggers like this: