Illegal terry stop, what can I do?

A police officer stopped me on the street tonight after I'd gotten back
from a long night at work; it was about 2:30am. While I was walking to
my house from where I'd parked, an officer in a patrol car started
pacing me. Just as I was about to get to my house, the officer inside
rolled down his window and told me to stop. He got out of his car,
approached me, and asked whether I had any ID on me (which he never
asked to see, incidentally.) He asked me where I lived, what I did for
a living, and what I was doing ("over there [pointing to my house]",
"program computers" and "coming back from a late night at work",

He demanded that I empty my pockets onto the hood of his patrol car. I
said I would comply, and while doing that, I asked him whether he had
any reasonable suspicion for searching me. He said that he wanted to
make sure I "wasn't going to pull out a knife while [he] patted [me]
down" and that it was for "his safety." He told me to put my hands on
the patrol car. I did, and he patted me down. When he didn't find
anything, he walked to the sidewalk and said he was going to look for
anything I'd "dumped." He then said that I had "looked nervous," and
that he wanted to make sure I wasn't breaking into any cars. I asked
him for his badge number. I got it and walked away.

Now, as I understand it, ordering me to empty my pockets isn't
something the police can order me to do under Terry v. Ohio, making
the officer's order an illegal search. If that is the case, is there
anything I can DO about it?

Also, is there anything I could have done differently? As I understand
it, refusing to obey the police offer would have landed me in trouble
regardless of whether his order was legal, right?