Some changes according to Lqi's remarks:
- some renaming.
- use some dyn_cast_or_null instead of dyn_cast.
- add some const.
- avoid comparison with literal boolean.
- Add some unitTest for swap.