Is temporarily overriding equals method possible? - java-7

Is there a way to temporarily override hashcode and equals in java ?
I have a User class with id, name and age attributes. The current equals compares the id, rightly so. So if two ids, are same then the Users are equal.
Now, I have two Lists of User and I want to find users with common names.
Other than looping every element of list1 over every element of list2, I have no other option (java 7). Is there a way, I can temporarily change the behaviour of equals ? In this case, where I am coding the logic for searching users with common names, I want to be able to say if name is same then the users are equal (even though its technically incorrect), without touching the actual User class's equal.

Related

Why is the EntryID Changing in VSTO? The MailItem is not moving folders

I'm writing some code in C# that matches a pattern from the subject and then ingests the email. To initialize my datastore, I go through the current Microsoft.Office.Interop.Outlook.Table.
while (!table.EndOfTable)
{
Row row = table.GetNextRow();
string entryId = row["EntryID"].ToString();
this.SaveInXML(entryId, row);
}
It seems pretty simple. Well, I also have an event (Application.ItemLoad) that I'm watching, too. I notice that in the event the MailItem's EntryID is completely different than the Table's EntryID. In fact, the string lengths are not even the same (See example below). Why is this? Shouldn't they be the same? The item has not moved folders, so I'd assume it's the same. Thank you, all.
Example code:
NameSpace ns = this.Folder.Application.GetNamespace("MAPI");
var mi = ns.GetItemFromID("EF0000003E65593F1D361C44AFBFA24E6F365D6E04782F00") as MailItem;
string entryId = mi.EntryID;
System.Diagnostics.Debug.WriteLine("EF0000003E65593F1D361C44AFBFA24E6F365D6E04782F00");
System.Diagnostics.Debug.WriteLine(entryId);
// Output Produced:
// EF0000003E65593F1D361C44AFBFA24E6F365D6E04782F00
// 000000003E65593F1D361C44AFBFA24E6F365D6E0700CC348F1AD97A224B9898503750437E4700000000010C0000CC348F1AD97A224B9898503750437E470000F59160590000
//
// Notice that the second WriteLine isn't even remotely close to the EntryID that I requested.
Entry identifiers come in two types: short-term and long-term.
Short-term entry identifiers are faster to construct, but their uniqueness is guaranteed only over the life of the current session on the current workstation.
Long-term entry identifiers have a more prolonged lifespan. Short-term entry identifiers are used primarily for rows in tables and entries in dialog boxes, whereas long-term entry identifiers are used for many objects such as messages, folders, and distribution lists.
Use the MailItem.EntryID property if you need to get a long-term entry identifiers.
Entry identifiers cannot be compared directly because one object can be represented by two different binary values. Use the NameSpace.CompareEntryIDs method to determine whether two entry identifiers represent the same object.
As Eugene noted, you have two kinds of entry ids - long term and short term. Even for long-term entry ids, they can be different depending on how the item was opened. Long term entry ids always start with "00000000". Short term entry ids can only be used in the current MAPI session and therefore should not be persisted to be used across different sessions.
You must treat entry id as black boxes and never compare them directly - always use Namespace.CompareEntryIDs.

Logic for parsing names

I am wanting to solve this problem, but am kind of unsure how to correctly structure the logic for doing this. I am given a list of user names and I am told to find an extracted name for that. So, for example, I'll see a list of user names such as this:
jason
dooley
smith
rob.smith
kristi.bailey
kristi.betty.bailey
kristi.b.bailey
robertvolk
robvolk
k.b.dula
kristidula
kristibettydula
kristibdula
kdula
kbdula
alexanderson
caesardv
joseluis.lopez
jbpritzker
jean-luc.vey
dvandewal
malami
jgarciathome
christophertroethlisberger
How can I then turn each user name into an extracted name? The only parameter I am given is that every user name is guaranteed to have at least a partial person's name.
So for example, kristi.bailey would be turned into "Kristi Bailey"
alexanderson would be turned into "Alex Anderson"
So, the pattern I see is that, if I see a period I will turn that into two strings (possibly a first and last name). If I see three periods then it will be first, middle. The problem I am having trouble finding the logic for is when the name is just clumped up together like alexanderson or jgarciathome. How can I turn that into an extracted name? I was thinking of doing something like if I see 2 consonants and a vowel in a row I would separate the names, but I don't think that'll work.
Any ideas?
I'd use a string.StartsWith method and a string.EndsWith method and determine the maximum overlap on each. As long as it's more than 2 characters, call that the common name. Sort them into buckets based on the common name. It's a naive implementation, but it that's where I'd start.
Example:
string name1 = "kristi.bailey";
string name2 = "kristi.betty.bailey";
// We've got a 6 character overlap for first name:
name2.StartsWith(name1.Substring(0,6)) // this is true
// We've got a 6 character overlap for last name:
name2.EndsWith(name1.Substring(7)) // this is true
HTH!

Trust based dictionary

Lets say we have a system with a number of "users". Each of those users can have their own "key/value" dictionary.
Let say we store this data in a dictionary itself, data, with key (user, key) and value value.
Also, users can trust other users. Lets say we store this in trust, with key user and value which is an ordered array of users that user trusts.
Lets say "Alice" trusts "Bob" the most, but then "Chris".
So we store in trust: "Alice": ["Bob", "Chris"]
I then want a function get, which works like the following:
get(user, key)
if (data.haskey((user, key)) then
return data.get((user,key))
else
trusted_users = trust.get(user)
for (trusted_user in trusted_users)
result = get(trusted_user, key)
if (result is not null)
return result
return null
end if
In plain English, if I've got the key in my store, return it's value. If not, check my most trusted friend (and anyone who they trust, and so on). If not, then check my next most trusted friend and so on. If none of my friends are friends (or friends of friends etc) of someone who has this key, then return that it's not found.
I've given a trivial implementation to explain my problem, however it won't scale well, as due to the recursion it's quite possible it execution time increases linearly with the number of users.
I'm looking for a better data design and algorithm which scales better.
The code presented is worse than linear, because it is possible to visit friends A-B-C and then A-C-B, if A, B, and C, are all friends. In the recursive friend search you should keep a set of friends visited so far, and not recurse through a friend you have already visited. You would have to pass a pointer to this set through as part of the recursive call to get it updated properly.
If values in the map do not change and keys may be accessed again, you could keep a copy of key-value pairs from friends to save bothering them again.

Magento check quote has order

Question:
Is there a sure way of checking if a Quote object has a related Order object without loading
the Order object?
Research
I have looked at the following ways, but I am not 100% they will be accurate:
is_active, so when an order is complete this field is set to 0, I am not sure that this is the only time it happens though.
reserved_order_id, the wording seems like it can potentially not fill the reserved order.
converted_at (thanks #Marius), always seems to be null for me.
subtotal, this looks interesting, if the quote has a subtotal surely it has been ordered.
I guess I could set a flag and add it to the convert_quote_to_order observer, but there must be a simple inbuilt way of doing this, really I don't want the extra overhead of joining the order object onto my collection when I am doing then check.
Many thanks.
Check the field converted_at ($quote->getConvertedAt()). If it's null it means it doesn't have an order. If it has an order this should be the date that the order was created.
[Edit]
Ok. I've checked and I can confirm that is_active is a reliable way to check if a quote has an associated order. If the value is 0 the quote has an order.
The flag is set to 0 by the method Mage_Sales_Model_Service_Quote::_inactivateQuote and this method is called when placing an order:
Mage_Sales_Model_Service_Quote::submitOrder
Mage_Sales_Model_Service_Quote::submitNominalItems
Mage_Sales_Model_Service_Quote::submitAll.
On the other hand, reserved_order_id is not a reliable flag for quotes converted to orders. It can be filled in with a value but the order can be missing. I've had in a couple of occasions to write a module where I've reserved the order id before the order was placed.

PROPERTYKEY::pid Meaning

Can someone explain what the pid field in a PROPERTYKEY structure is? Microsoft says just don't use 0 or 1 and you're fine, but this doesn't help when I need to implement IPropertyStore in my code. Is the pid supposed to be part of the key, so multiple values with the same fmtid but different pid may be present? Or should it be ignored, so GetValue should return any value with a matching fmtid, ignoring pid?
This fmtid+pid combination is historically related to OLE (yes, that's pretty old).
At that time, the fmtid (format id) was like a category, and the pid (property id) was the property identifier in the category. For example, you have here the first FMTID defined: Predefined Property Set Format Identifiers. These properties are still used for Office documents (Author, Keywords, etc.) So you had many properties per category (so few fmtid for a lot of properties), but the combination of both fmtid and pid always make the property unique across space and galaxies.
Others FMTID appeared since, you can have a look at in in Windows SDK's propkey.h: FMTID_AudioSummaryInformation, FMTID_Volume, FMTID_ShellDetails, etc...
Today, for some new properties, the FMTID does not mean anything anymore. For example, the System.Contact.Birthday has a fmtid of 176DC63C-2688-4E89-8143-A347800F25E9 and an id of 47, but the fmtid has no special meaning, and is not defined specifically, so it in fact could be used solely for the key.
So, for a given property, you must consider the key is still the combination of both (hence the structure name: PROPERTYKEY), but you could define your own properties with pid as something greater or equal to 2 (as the official doc specifies) and fmtid as a new guid if you prefer. I personally still prefer to define one common FMTID for a group of properties.

Resources